Thread Verwendung von scalar (@list) in Schleifen wenn Listenlänge konstant ist?
(8 answers)
Opened by mika at 2016-05-17 14:43
Mit scalar innerhab jeder Schleifeniteration wird's langsamer.
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 use warnings; use strict; use Benchmark "timethese"; my $count = 5000; my @array = qw( a b c ); my $glbArraySize = scalar( @array ); timethese( 10000, { "scalarSavedGlobally" => sub { for ( 1 .. $count ) { my $foo = 1 if $glbArraySize == $_; } }, "scalarSavedLocally" => sub { my $arraySize = scalar( @array ); for ( 1 .. $count ) { my $foo = 1 if $arraySize == $_; } }, "scalarNotSaved" => sub { for ( 1 .. $count ) { my $foo = 1 if scalar( @array ) == $_; } } } ); Ausgabe: Code: (dl
)
1 Benchmark: timing 10000 iterations of scalarNotSaved, scalarSavedGlobally, scalarSavedLocally... Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"
|