-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple.pl
117 lines (110 loc) · 2.51 KB
/
simple.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use strict;
use warnings;
use Benchmark qw(:all);
use Cache::FastMmap;
use Cache::LRU;
use Cache::Ref::LRU;
use Cache::Ref::Util::LRU::Array;
use Cache::Ref::Util::LRU::List;
use Tie::Cache::LRU;
my $size = 1024;
my $loop = 1000;
sub cache_hit {
my $cache = shift;
$cache->set(a => 1);
my $c = 0;
$c += $cache->get('a')
for 1..$loop;
$c;
}
print "cache_hit:\n";
cmpthese(-1, {
'Cache::FastMmap' => sub {
cache_hit(
Cache::FastMmap->new(
cache_size => '1m',
),
);
},
'Cache::FastMmap (raw)' => sub {
cache_hit(
Cache::FastMmap->new(
cache_size => '1m',
raw_values => 1,
),
);
},
'Cache::LRU' => sub {
cache_hit(
Cache::LRU->new(
size => $size,
),
);
},
'Cache::Ref::LRU (Array)' => sub {
cache_hit(
Cache::Ref::LRU->new(
size => $size,
lru_class => qw(Cache::Ref::Util::LRU::Array),
),
);
},
'Cache::Ref::LRU (List)' => sub {
cache_hit(
Cache::Ref::LRU->new(
size => $size,
lru_class => qw(Cache::Ref::Util::LRU::List),
),
);
},
'Tie::Cache::LRU' => sub {
tie my %cache, 'Tie::Cache::LRU', $size;
$cache{a} = 1;
my $c = 0;
$c += $cache{a}
for 1..$loop;
$c;
},
});
print "\ncache_set:\n";
srand(0);
my @keys = map { int rand(1048576) } 1..65536;
sub cache_set {
my $cache = shift;
$cache->set($_ => 1)
for @keys;
$cache;
}
cmpthese(-1, {
# no test for Cache::FastMmap since it does not have the "size" parameter
'Cache::LRU' => sub {
cache_set(
Cache::LRU->new(
size => $size,
),
);
},
# too slow
#'Cache::Ref::LRU (Array)' => sub {
# cache_set(
# Cache::Ref::LRU->new(
# size => $size,
# lru_class => qw(Cache::Ref::Util::LRU::Array),
# ),
# );
#},
'Cache::Ref::LRU (List)' => sub {
cache_set(
Cache::Ref::LRU->new(
size => $size,
lru_class => qw(Cache::Ref::Util::LRU::List),
),
);
},
'Tie::Cache::LRU' => sub {
tie my %cache, 'Tie::Cache::LRU', $size;
$cache{$_} = 1
for @keys;
\%cache;
},
});