in reply to Pass by ref vs alias : why does scalar size matter?

Very interesting thing...
I'm getting similar results like you (v5.8.8 built for i486-linux-gnu-thread-multi) when I run your benchmark.
But when I change now s/\s+//; to s/\s+/ /; then 'ref' is always ca. 30% slower than 'alias'! It looks that this might be dependent on the performed operation.

Replies are listed 'Best First'.
Re^2: Pass by ref vs alias : why does scalar size matter?
by clinton (Priest) on May 01, 2008 at 22:22 UTC

    When I tried your example of changing s/\s+// to s/\s+/ / I still saw the decline in speed in the alias version, albeit more slowly. The ref version went from -40% to -20%.

    I saw a similar (but varying) decline for these operations:

    $_[0] .= 'abcde'; vs ${ $_[0] } .= 'abcde'; $_[0] = substr( $_[0] , -500); vs ${ $_[0] } = substr(${ $_[0] } , -500);

    Presumably this has something to do with writing the value back to an alias?

    Update: This is just wrong. When retesting this morning, I realised that, while the differences in speed between the two got closer together as the string grew, they never actually switched. Instead, it was just that the string operation took a proportionally greater part of the time, and thus the cost of using references was relatively less.

    The only example I've been able to come up with where the two actually switched is the original version in Pass by ref vs alias : why does scalar size matter?

      Presumably this has something to do with writing the value back to an alias?
      Good idea. I was testing an operation which doesn't change the variable, m/\s+/, which results in alias only about 2% faster than ref, independent from length.

      One thing I liked to test was the influence the change of the string length by the operation has. When I changed s/\s+// to s/\s+//g the difference is again about 2% in favor of alias, otherwise it's about the same for s/\s/_/g which doesn't change the length.