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; }
In reply to Re^2: Whats quicker - passing along as variable to sub, or global var?
by ultranerds
in thread Whats quicker - passing along as variable to sub, or global var?
by ultranerds
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |