Leser: 1
![]() |
|< 1 2 3 >| | ![]() |
28 Einträge, 3 Seiten |
KurtZ+2008-03-19 14:49:32--pq+2008-03-19 14:24:37--KurtZ+2008-03-19 13:50:41--Code (perl): (dl )cat beispiel.txt | perl -ne "print +(split /\|/,$_)[3],qq~\n~"
glückwunsch zum Useless-Use-Of-Cat award.
Tatsache? Vielleicht solltest du deine Alternative noch mal ausprobieren, bevor du den Preis aushändigst... :-)
pq+2008-03-19 15:31:02--meine güte, KurtZ, nun nimm den award schon an =)
perl -nle 'print [ split /\|/ ]->[2] ' beispiel.txt
KurtZ+2008-03-19 15:47:16--Außerdem optimiert das der Compiler wirklich dahingehend dass nicht mehr die ganze Zeile gesplittet wird sondern nach idx abgebrochen wird?
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
use strict; $\="\n"; $,="\t"; my $str=join ",",('a'..'z')x10000; my $idx=26; #print $str; my $codes={ 'namedArr' => sub { my @F=split /,/ , $str ; return $F[$idx]; }, 'anoArr' => sub { return +(split /,/, $str) [$idx]; }, 'anoArrRef' => sub { return [ split /,/, $str ] -> [$idx]; }, }; #- Test Codes while ( my ($name, $cr) = each %$codes ) { print $name.":", $cr->(); } #- Benchmark use Benchmark qw(cmpthese); cmpthese( 10, $codes);
1
2
3
4
Rate namedArr anoArrRef anoArr
namedArr 0.762/s -- -25% -53%
anoArrRef 1.02/s 34% -- -36%
anoArr 1.61/s 111% 57% --
KurtZ+2008-03-19 15:47:16--vorher sagt mir bitte aber auch was das + in +(split ...)[idx] genau macht.
Habe schon scalar geschrieben aber des passt nicht.
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...
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); }
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% --
![]() |
|< 1 2 3 >| | ![]() |
28 Einträge, 3 Seiten |