usage.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <library/cpp/comptable/comptable.h>
  2. #include <util/random/random.h>
  3. #include <util/random/fast.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. using namespace NCompTable;
  7. template <bool HQ>
  8. void DoTest(const TCompressorTable& table, const TVector<TString>& lines) {
  9. TVector<char> compressed;
  10. TVector<char> decompressed;
  11. TChunkCompressor compressor(HQ, table);
  12. TChunkDecompressor deCompressor(HQ, table);
  13. size_t origSize = 0;
  14. size_t compSize = 0;
  15. float cl1 = clock();
  16. for (size_t i = 0; i < lines.size(); ++i) {
  17. const TString& line = lines[i];
  18. compressor.Compress(line, &compressed);
  19. origSize += line.size();
  20. compSize += compressed.size();
  21. TStringBuf in(compressed.data(), compressed.size());
  22. deCompressor.Decompress(in, &decompressed);
  23. if (decompressed.size() != line.size() || memcmp(decompressed.data(), line.data(), decompressed.size())) {
  24. Cout << i << "\n";
  25. Cout << line << "\n"
  26. << TString(decompressed.data(), decompressed.size()) << "\n";
  27. abort();
  28. }
  29. }
  30. float cl2 = clock();
  31. float secs = (cl2 - cl1) / CLOCKS_PER_SEC;
  32. Cout << "origSize: " << origSize << "\tcompSize: " << compSize << Endl;
  33. Cout << "yep! compression + decompression speed " << origSize / 1024.0f / 1024.0f / secs << " mbps\n";
  34. Cout << "yep! compression ratio " << double(origSize) / double(compSize + 1) << "\n";
  35. }
  36. int main(int argc, const char* argv[]) {
  37. TReallyFastRng32 rr(17);
  38. TVector<TString> lines;
  39. /*FILE *fp = fopen("res", "rb");
  40. while (!feof(fp)) {
  41. char buff[4096];
  42. fscanf(fp, "%s", buff);
  43. lines.push_back(TString(buff));
  44. }*/
  45. //for (size_t i = 0; i < 10000000; ++i) {
  46. //for (size_t i = 0; i < 1000000; ++i) {
  47. for (size_t i = 0; i < 1000000; ++i) {
  48. size_t size = rr.Uniform(32);
  49. TString res = "www.yandex.ru/yandsearch?text=";
  50. for (size_t j = 0; j < size; ++j) {
  51. res += "qwer"[rr.Uniform(4)];
  52. }
  53. lines.push_back(res);
  54. }
  55. THolder<TDataSampler> sampler(new TDataSampler);
  56. for (size_t i = 0; i < lines.size(); ++i) {
  57. sampler->AddStat(lines[i]);
  58. }
  59. TCompressorTable table;
  60. sampler->BuildTable(table);
  61. DoTest<true>(table, lines);
  62. DoTest<false>(table, lines);
  63. Y_UNUSED(argc);
  64. Y_UNUSED(argv);
  65. return 0;
  66. }