Thread best. Feld speichern (wie z.B. bei awk) (27 answers)
Opened by Duff at 2008-03-19 11:53

KurtZ
 2008-03-19 18:44
#107273 #107273
User since
2007-12-13
411 Artikel
BenutzerIn
[default_avatar]
renee+2008-03-19 17:28:32--
print kannst Du auch mit Klammern aufrufen (z.B. print("Hallo")) und damit perl nicht denkt dass das (split /.../) zum Aufruf gehört, schreibt man das + davor. Ist irgendwo bei den Referenzen glaube ich dokumentiert...


argh... da war ich mal wieder auf dem total falschen Dampfer... und das obwohl ich letztens die Einsichten des "ppi"-Autors durchgelesen habe...

(Ich verstehe immer besser wieso in NRW Python(!) im Lehrplan zugelassen ist.)

BTW split kennt ein Limit-Arg damit wirds unschlagbar:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use strict;
$\="\n";
$,="\t";


my $str;
my $idx=25;
sub idx {25};



our $str_mult=10000;
our $repeats;
for  $repeats (100,1000,10000) {
        bench();
}


sub bench {
        my $str_repeats=$str_mult/$repeats;
        my $str=join ",",('a'..'z')x$str_repeats;

        print "\n\n\n--- Benchmarking with";
        print "Stringlength: ",length($str);
        print "Repeats: ",$repeats;

        #- Define Codes
        my $codes={
                'namedArr' =>
                        sub {
                                my @F=split /,/ , $str ;
                                return $F[idx];
                        },
                'anoArr_plus' =>
                        sub {
                                return +(split /,/, $str) [$idx];
                        },

                'anoArr' =>
                        sub {
                                return @{[ split /,/, $str]} [$idx];
                        },
                'anoArrRef' =>
                        sub {
                                return [ split /,/, $str ] -> [$idx];
                        },

                'split_lim' =>
                        sub {
                                return (split /,/, $str, $idx+2) [$idx];
                                },
#               'regEx' =>
#                       sub {
#                               no strict qw/refs/;
#                               $str =~ m/(.*?:,){$idx}/g;
#                               return ${$idx-1};
#               },
                
                #'tst_err' => sub {"xyz"},

        };


        
        #- Test Codes
        my ($ret,$retalt);
        while ( my ($name, $cr) = each %$codes ) {
                $ret=$cr->();
                # meckern bei unterschiedlichen Returns
                if ( $retalt and  $retalt ne $ret ) {
                        print  "ERROR with sub $name, returned '$ret'";
                }
                $retalt=$ret;
# print "sub $name, returned", $ret;
        }


        #- Benchmark
        use Benchmark qw(cmpthese);
        cmpthese( $repeats, $codes);
}
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
--- Benchmarking with
Stringlength: 5199
Repeats: 100
(warning: too few iterations for a reliable count)

(warning: too few iterations for a reliable count)

Rate namedArr anoArrRef anoArr anoArr_plus split_lim
namedArr 83.3/s -- -28% -32% -57% -100%
anoArrRef 116/s 40% -- -6% -40% -100%
anoArr 123/s 48% 6% -- -36% -100%
anoArr_plus 192/s 131% 65% 56% -- -100%
split_lim 100000000000000000/s 120000000000000000% 86000000000000000% 81000000000000016% 52000000000000032% --
TMTOWTDYOG (there's more than one way to dig your own grave)

View full thread best. Feld speichern (wie z.B. bei awk)