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?

Hi,

Thanks - I was actually doing some benchmarks after I wrote this :) Here are the results:

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>
The 1st one uses:
my $new_string = testing_string($string); sub testing_string { my $content = $_[0]; # do some stuff $content =~ s/a/b/sg; return $content; }
..where as the "slower" one, 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; }
>> Then you should avoid making copies. Note that passing strings to functions does not make a copy (@_ aliases), but my $long_content = $_[0]; does.

Not sure what you mean?

TIA

Andy

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
    Not sure what you mean?

    I meant exactly what I wrote: Passing a string to a function doesn't make a copy. Assigning it to a separate variable does. So

    sub f { print $_[0]; }

    avoids the copy, whereas

    sub f { my $x = $_[0]; # copy created here print $x; }

    creates one.

    Since it's quite ugly to use $_[NUMBER] all the time, I suggested references instead.

      Aaaah ok - missed that reply =) That would explain why testing1() is considerably quicker than testing2() in the below:

      sub testing1 { print "At testing1 \n"; $_[0] =~ s/foo/test/g; } sub testing2 { print "At testing2 \n"; my $string = $_[0]; $string =~ s/foo/test/g; return $string; }
      Thanks!

      Andy
        Notice that in testing1() you're modifying the original string, so it's very likely the substitution does just plain nothing in all except the very first run.

        In testing2(), you're starting with a new string every time.

        So, that's probably skewing the benchmark.

Re^3: Whats quicker - passing along as variable to sub, or global var?
by jethro (Monsignor) on Apr 08, 2011 at 14:04 UTC
    my $new_string = testing_string($string); sub testing_string { my $content = $_[0]; #string copy, expensive # do some stuff $content =~ s/a/b/sg; return $content; #string copy, expensive }
    # do stuff here my %test_var; $test_var{$string} = $string; # double!! string copy and hash # sum calculation, expensive. And why # this senseless copying into a hash? a scalar # variable (i.e $string) is global too testing_string(); $string = $test_var{$string}; # string copy and hash sum calculati +on of $string sub testing_string_2 { # do some stuff here $test_var{$string} =~ s/a/b/sg; }

    What you could do is this:

    testing_string(\$string); #send a reference to $string instead of $ +string sub testing_string { my $contentref = $_[0]; # do some stuff $$contentref =~ s/a/b/sg; }

    But are you sure you have problems with the speed of your script? Did you profile your script to see where it is slow? You might google for "premature optimizations", this is the most common mistake programmers make. And as you can see, often they even make it more expensive because they don't know enough about the underlying mechanism.

    Simply copying strings is a very fast operation on modern CPUs because they can use special instructions (maybe even DMA transfers) for it. You might only save micro- or milliseconds if this operation is not done thousand or million times in your script