Thread foreach beschleunigen
(22 answers)
Opened by bianca at 2009-01-01 16:40
naja, probieren geht über studieren...
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 use Benchmark; my %hash; my ($c, $i) = 1000, 100; for (1 .. $c) { $hash{"test$_"} = $_; $hash{"foo$_"} = $_; } sub SUBSTR { foreach my $bid (sort { substr ($a,4) <=> substr ($b,4) } grep {/^test(\d+)$/} keys %hash) { } } sub SCHWARTZ { for my $bid (map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { m/^test(\d+)$/ ? [$1, $_] : () } keys %hash) { } } timethese($i, { SUBSTR => \&SUBSTR, SCHWARTZ => \&SCHWARTZ }); Code: (dl
)
1 Benchmark: Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wie frage ich & perlintro brian's Leitfaden für jedes Perl-Problem |