Leser: 1
|< 1 2 >| | 12 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/perl
use Benchmark qw(:all :hireswallclock);
$count = 1000;
for(1..$count){
$hash{$_} = $_;
}
timethese($count, {
'each' => sub {
while (my ($key, $value) = each %hash) {
$hash{$key} = 1;
}
},
'foreach keys' => sub {
foreach (keys %hash) {
$hash{$_} = 1;
}
},
'for keys' => sub {
for (keys %hash) {
$hash{$_} = 1;
}
},
} );
1
2
3
4
Benchmark: timing 1000 iterations of each, for keys, foreach keys...
each: 3.46613 wallclock secs ( 3.40 usr + 0.01 sys = 3.41 CPU) @ 292.83/s (n=1000)
for keys: 1.98373 wallclock secs ( 1.97 usr + 0.00 sys = 1.97 CPU) @ 507.10/s (n=1000)
foreach keys: 2.02901 wallclock secs ( 1.98 usr + 0.00 sys = 1.98 CPU) @ 504.54/s (n=1000)
|< 1 2 >| | 12 Einträge, 2 Seiten |