in reply to Re: Whats quicker - passing along as variable to sub, or global var?
in thread Whats quicker - passing along as variable to sub, or global var?
The 1st one uses:C:\Users\Andy\Documents>perl test.pl Length of string: 101000000 Time taken was 0 wallclock secs ( 0.17 usr 0.03 sys + 0.00 cusr + 0.00 csys = 0.20 CPU) seconds Time taken was 1 wallclock secs ( 1.13 usr 0.03 sys + 0.00 cusr + 0.00 csys = 1.16 CPU) seconds C:\Users\Andy\Documents>
..where as the "slower" one, does:my $new_string = testing_string($string); sub testing_string { my $content = $_[0]; # do some stuff $content =~ s/a/b/sg; return $content; }
>> Then you should avoid making copies. Note that passing strings to functions does not make a copy (@_ aliases), but my $long_content = $_[0]; does.# do stuff here my %test_var; $test_var{$string} = $string; testing_string(); $string = $test_var{$string}; sub testing_string_2 { # do some stuff here $test_var{$string} =~ s/a/b/sg; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Whats quicker - passing along as variable to sub, or global var?
by moritz (Cardinal) on Apr 08, 2011 at 13:45 UTC | |
by ultranerds (Hermit) on Apr 08, 2011 at 13:50 UTC | |
by bart (Canon) on Apr 08, 2011 at 21:33 UTC | |
|
Re^3: Whats quicker - passing along as variable to sub, or global var?
by jethro (Monsignor) on Apr 08, 2011 at 14:04 UTC |