Thread foreach beschleunigen (22 answers)
Opened by bianca at 2009-01-01 16:40

pq
 2009-01-01 17:19
#129923 #129923
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
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
2
3
4
5
6
7
8
Benchmark: 
timing 100 iterations of
SCHWARTZ, SUBSTR
...

SCHWARTZ: 1 wallclock secs ( 0.77 usr + 0.00 sys = 0.77 CPU) @ 129.87/s (n=100)

SUBSTR: 1 wallclock secs ( 0.91 usr + 0.00 sys = 0.91 CPU) @ 109.89/s (n=100)
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: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread foreach beschleunigen