Thread Hashkey finden (8 answers)
Opened by bianca at 2013-08-24 09:00

GwenDragon
 2013-08-24 11:35
#169733 #169733
User since
2005-01-17
14773 Artikel
Admin1
[Homepage]
user image
Ja, die Liste gleich in map zurück geben ist effizienter (ca. 8% schneller).
Code (perl): (dl )
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
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

use Benchmark qw(:all);

my %test = (
    sup     => [
        {
            name            => 'Fooname',
            ident           => 'foo',
        },
        {
            name            => 'barname',
            ident           => 'bar',
        },
    ],
);

print cmpthese(1000_000, {
        'map+grep' => sub { 
                        my ($match) = 
                        map{$_->{name}}
                        grep{$_->{ident}eq'bar'} 
                        @{$test{sup}};
                },
        'map list' => sub {
                        my ($match) = 
                        map{$_->{ident}eq'bar'?($_->{name}):()} 
                        @{$test{sup}};
                },
    });


__END__

             Rate map+grep map list
map+grep 529661/s       --      -7%
map list 572410/s       8%       --

View full thread Hashkey finden