is there a quick way to exchange the values of two vars without using a third one?

Just a thought but you need to be a little more specific about your definition of quick. If its "quick to type" then yes as the other respondants said you can do

($x,$y)=($y,$x);
If its "quick to execute" then no there is not, as the following benchmark shows
use Benchmark 'cmpthese'; use strict; my ($x,$y,$u)=(1,2); sub swapL{ ($x,$y)=($y,$x); } sub swapTt{ my $t=$x; $x=$y; $y=$t; } sub swapTu{ $u=$x; $x=$y; $y=$u; } cmpthese (-10,{list =>\&swapL, mytmp=>\&swapTt, glbtmp=>\&swapTu}); __END__ Benchmark: running glbtmp, list, mytmp, each for at least 10 CPU secon +ds... glbtmp: 11 wallclocks (10.01 usr + 0.00 sys = 10.01 CPU) @ 106353 +0.80/s (n=10651261) list: 13 wallclocks (10.19 usr + 0.00 sys = 10.19 CPU) @ 65633 +1.40/s (n= 6686048) mytmp: 9 wallclocks (10.27 usr + 0.00 sys = 10.27 CPU) @ 90580 +7.23/s (n= 9299017) Rate list mytmp glbtmp list 656331/s -- -28% -38% mytmp 905807/s 38% -- -15% glbtmp 1063531/s 62% 17% --
A little explaination of this might be useful. When you use the list assignment you are secretly asking perl to create (in this case) two temporary variables (as well as do a bunch of overhead like figuring out how many values it actually has to copy), dynamically, and then to assign those variables to the left side. So while it may look like less code it is in fact much much more. This is nice if you need to swap a couple of variables or reorder a list (check out list slices) in a non-time critical part of your application, but when you need to do the same inside a heavily utilized routine (such as when sorting) then using the less elegant scalar temporary is both faster and has less memory overhead.

And lets not hear about "premature optimization", there are times when you really really will want the the 40%(!) gain. And there are times when you wont... :-) Knowing the difference is the hard part...

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.


In reply to Quick in execution or typing? (Re: Lost when exchanging values) by demerphq
in thread Lost when exchanging values by Sigmund

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.