Skip to content

Instantly share code, notes, and snippets.

@karupanerura
Created September 2, 2011 05:20
Show Gist options
  • Save karupanerura/1187967 to your computer and use it in GitHub Desktop.
Save karupanerura/1187967 to your computer and use it in GitHub Desktop.
Compress::LZW VS Compress::LZF VS Compress::LZO VS Compress::Zlib(memGzip) VS Compress::Snappy
use common::sense;
use Benchmark qw(timethese cmpthese);
use List::Util qw/max/;
use Compress::LZO ();
use Compress::LZF ();
use Compress::LZW ();
use Compress::Zlib ();
use Compress::Snappy ();
my $data = 'data' x 10000;
my %algorithm = (
lzf => +{
compress => \&Compress::LZF::compress,
decompress => \&Compress::LZF::decompress,
},
lzo => +{
compress => \&Compress::LZO::compress,
decompress => \&Compress::LZO::decompress,
},
lzw => +{
compress => \&Compress::LZW::compress,
decompress => \&Compress::LZW::decompress,
},
gzip => +{
compress => \&Compress::Zlib::memGzip,
decompress => \&Compress::Zlib::memGunzip,
},
snappy => +{
compress => \&Compress::Snappy::compress,
decompress => \&Compress::Snappy::decompress,
},
);
my $name_maxlength = max map { length($_) } keys %algorithm;
my $sizetext_length = length(length($data));
{# compress test
use Test::More;
plan tests => scalar(keys %algorithm);
foreach my $name (keys %algorithm) {
is $algorithm{$name}->{decompress}->($algorithm{$name}->{compress}->($data)), $data, sprintf("\%${name_maxlength}s compress/decompress", $name);
}
}
{# compress size
my %compressed_size;
foreach my $name (keys %algorithm) {
$compressed_size{$name} = length($algorithm{$name}->{compress}->($data));
}
say sprintf("\%${name_maxlength}s size = \%${sizetext_length}d(%6.2f%%)", 'raw', length($data), 100 * length($data)/length($data));
foreach my $name (sort { $compressed_size{$b} <=> $compressed_size{$a} } keys %compressed_size) {
say sprintf("\%${name_maxlength}s size = \%${sizetext_length}d(%6.2f%%)", $name, $compressed_size{$name}, 100 * $compressed_size{$name}/length($data));
}
}
{# compress speed
my %bench_code;
foreach my $name (keys %algorithm) {
$bench_code{$name} = sub {
my $comp = $algorithm{$name}->{compress}->($data);
my $decomp = $algorithm{$name}->{decompress}->($comp);
die('unmutch!') unless($data eq $decomp);
};
}
cmpthese timethese(10000, \%bench_code);
}
__END__
% perl compress_bench.pl
1..5
ok 1 - snappy compress/decompress
ok 2 - lzo compress/decompress
ok 3 - gzip compress/decompress
ok 4 - lzw compress/decompress
ok 5 - lzf compress/decompress
raw size = 40000(100.00%)
snappy size = 1888( 4.72%)
lzw size = 1128( 2.82%)
lzf size = 468( 1.17%)
lzo size = 177( 0.44%)
gzip size = 78( 0.20%)
Benchmark: timing 10000 iterations of gzip, lzf, lzo, lzw, snappy...
gzip: 21 wallclock secs (19.03 usr + 0.02 sys = 19.05 CPU) @ 524.81/s (n=10000)
lzf: 2 wallclock secs ( 2.09 usr + 0.00 sys = 2.09 CPU) @ 4776.12/s (n=10000)
lzo: 3 wallclock secs ( 1.95 usr + 0.00 sys = 1.95 CPU) @ 5140.56/s (n=10000)
lzw: 460 wallclock secs (414.45 usr + 0.02 sys = 414.47 CPU) @ 24.13/s (n=10000)
snappy: 1 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 9922.48/s (n=10000)
Rate lzw gzip lzf lzo snappy
lzw 24.1/s -- -95% -99% -100% -100%
gzip 525/s 2075% -- -89% -90% -95%
lzf 4776/s 19696% 810% -- -7% -52%
lzo 5141/s 21206% 880% 8% -- -48%
snappy 9922/s 41026% 1791% 108% 93% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment