Thread Strings: und Variablen (11 answers)
Opened by dax42 at 2004-06-17 16:56

Oesi50
 2004-06-22 15:17
#83537 #83537
User since
2004-05-15
33 Artikel
BenutzerIn
[default_avatar]
[quote=renee,19.06.2004, 12:15]Ja, each ist noch schneller...[/quote]
Code: (dl )
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;
}
},
} );


Code: (dl )
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)
\n\n

<!--EDIT|Oesi50|1087903747-->

View full thread Strings: und Variablen