19
19
# include < lz4/lz4.h>
20
20
#endif
21
21
22
+ #define USE_RC
23
+ #define USE_ADAPTIVE
24
+ #define USE_SLZ4
25
+ #define USE_ZLIB
26
+
27
+ // #undef USE_LZ4
28
+
22
29
class Timer
23
30
{
24
31
public:
@@ -46,9 +53,11 @@ long long Timer::microseconds() const
46
53
return std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_).count ();
47
54
}
48
55
56
+ #ifdef USE_SLZ4
49
57
int def_slz4 (cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcoder::u8* src)
50
58
{
51
- cpprcoder::s32 result = slz4::compress (outStream.capacity (), reinterpret_cast <slz4::u8*>(&outStream[0 ]), srcSize, reinterpret_cast <slz4::u8*>(src));
59
+ slz4::SLZ4Context context;
60
+ cpprcoder::s32 result = slz4::compress (context, outStream.capacity (), reinterpret_cast <slz4::u8*>(&outStream[0 ]), srcSize, reinterpret_cast <slz4::u8*>(src));
52
61
53
62
if (0 <= result) {
54
63
outStream.resize (result);
@@ -64,7 +73,9 @@ int inf_slz4(cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcod
64
73
}
65
74
return result;
66
75
}
76
+ #endif
67
77
78
+ #ifdef USE_ZLIB
68
79
int def_zlib (cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcoder::u8* src)
69
80
{
70
81
int ret, flush;
@@ -156,6 +167,7 @@ int inf_zlib(cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcod
156
167
inflateEnd (&stream);
157
168
return ret == Z_STREAM_END ? outCount : Z_DATA_ERROR;
158
169
}
170
+ #endif
159
171
160
172
#ifdef USE_LZ4
161
173
int def_lz4 (cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcoder::u8* src)
@@ -177,6 +189,7 @@ int inf_lz4(cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcode
177
189
}
178
190
#endif
179
191
192
+ #ifdef USE_RC
180
193
void run_rangecoder (const char * filepath)
181
194
{
182
195
Timer timer;
@@ -223,7 +236,9 @@ void run_rangecoder(const char* filepath)
223
236
}
224
237
delete[] src;
225
238
}
239
+ #endif
226
240
241
+ #ifdef USE_ADAPTIVE
227
242
void run_adaptive (const char * filepath)
228
243
{
229
244
Timer timer;
@@ -281,7 +296,9 @@ void run_adaptive(const char* filepath)
281
296
}
282
297
delete[] src;
283
298
}
299
+ #endif
284
300
301
+ #ifdef USE_SLZ4
285
302
void run_slz4 (const char * filepath)
286
303
{
287
304
Timer timer;
@@ -312,19 +329,27 @@ void run_slz4(const char* filepath)
312
329
timer.stop ();
313
330
deflateTime = timer.microseconds ();
314
331
332
+ timer.start ();
315
333
if (inf_slz4 (decstream, encstream.size (), &encstream[0 ]) < 0 ) {
316
334
delete[] src;
317
335
return ;
318
336
}
319
337
timer.stop ();
320
338
inflateTime = timer.microseconds ();
321
339
340
+ for (size_t i=0 ; i<decstream.size (); ++i){
341
+ if (src[i] != decstream[i]){
342
+ return ;
343
+ }
344
+ }
322
345
double ratio = (double )encstream.size () / size;
323
346
// printf("|%s|%d|%d|%f|%lld|%lld|\n", filepath, size, encstream.size(), ratio, deflateTime, inflateTime);
324
347
printf (" |%s|%f|%lld|%lld|\n " , filepath, ratio, deflateTime, inflateTime);
325
348
delete[] src;
326
349
}
350
+ #endif
327
351
352
+ #ifdef USE_ZLIB
328
353
void run_zlib (const char * filepath)
329
354
{
330
355
Timer timer;
@@ -353,6 +378,7 @@ void run_zlib(const char* filepath)
353
378
timer.stop ();
354
379
deflateTime = timer.microseconds ();
355
380
381
+ timer.start ();
356
382
if (inf_zlib (decstream, encstream.size (), &encstream[0 ]) < 0 ) {
357
383
delete[] src;
358
384
return ;
@@ -365,6 +391,7 @@ void run_zlib(const char* filepath)
365
391
printf (" |%s|%f|%lld|%lld|\n " , filepath, ratio, deflateTime, inflateTime);
366
392
delete[] src;
367
393
}
394
+ #endif
368
395
369
396
#ifdef USE_LZ4
370
397
void run_lz4 (const char * filepath)
@@ -396,7 +423,8 @@ void run_lz4(const char* filepath)
396
423
}
397
424
timer.stop ();
398
425
deflateTime = timer.microseconds ();
399
-
426
+
427
+ timer.start ();
400
428
if (inf_lz4 (decstream, encstream.size (), &encstream[0 ]) < 0 ) {
401
429
delete[] src;
402
430
return ;
@@ -411,6 +439,7 @@ void run_lz4(const char* filepath)
411
439
}
412
440
#endif
413
441
442
+ #ifdef USE_RC
414
443
bool test_rangecoder ()
415
444
{
416
445
std::mt19937 mt;
@@ -439,7 +468,9 @@ bool test_rangecoder()
439
468
}
440
469
return true ;
441
470
}
471
+ #endif
442
472
473
+ #ifdef USE_ADAPTIVE
443
474
bool test_adaptive ()
444
475
{
445
476
std::mt19937 mt;
@@ -477,10 +508,11 @@ bool test_adaptive()
477
508
}
478
509
return true ;
479
510
}
511
+ #endif
480
512
481
513
int main (int /* argc*/ , char ** /* argv*/ )
482
514
{
483
- test_rangecoder ();
515
+ // test_rangecoder();
484
516
// test_adaptive();
485
517
486
518
const char * files[] =
@@ -499,25 +531,31 @@ int main(int /*argc*/, char** /*argv*/)
499
531
};
500
532
static const int numFiles = sizeof (files) / (sizeof (files[0 ]));
501
533
534
+ #ifdef USE_RC
502
535
printf (" Range Coder\n " );
503
536
printf (" -------------------------------------------\n " );
504
537
for (int i = 0 ; i < numFiles; ++i) {
505
538
run_rangecoder (files[i]);
506
539
}
540
+ #endif
507
541
542
+ #ifdef USE_ADAPTIVE
508
543
printf (" Adaptive Range Coder\n " );
509
544
printf (" -------------------------------------------\n " );
510
545
for (int i = 0 ; i < numFiles; ++i) {
511
546
run_adaptive (files[i]);
512
547
}
548
+ #endif
513
549
550
+ #ifdef USE_SLZ4
514
551
printf (" SLZ4\n " );
515
552
printf (" -------------------------------------------\n " );
516
553
for (int i = 0 ; i < numFiles; ++i) {
517
554
run_slz4 (files[i]);
518
555
}
556
+ #endif
519
557
520
- #if 1
558
+ #ifdef USE_ZLIB
521
559
printf (" ZLib\n " );
522
560
printf (" -------------------------------------------\n " );
523
561
for (int i = 0 ; i < numFiles; ++i) {
0 commit comments