Velocity Reviews > RE: No built-in swap function?

# RE: No built-in swap function?

Simon Wittber
Guest
Posts: n/a

 04-21-2004
For interest's sake, I wrote the following test:

import time
def tupleSwap(a,b):a,b = b,a
def tempSwap(a,b):
t = a
a = b
b = t
def xorSwap(a,b):
a ^= b
b ^= a
a ^= b
s = time.clock()
for i in xrange(1000000): tupleSwap(1,2)
t = time.clock() - s
print "1 million tuple swaps in", t, "seconds."
s = time.clock()
for i in xrange(1000000): tempSwap(1,2)
t = time.clock() - s
print "1 million temp swaps in", t, "seconds."
s = time.clock()
for i in xrange(1000000): xorSwap(1,2)
t = time.clock() - s
print "1 million xor swaps in", t, "seconds."

1 million tuple swaps in 1.78324228359 seconds.
1 million temp swaps in 1.53032270861 seconds.
1 million xor swaps in 2.17933312753 seconds.

Interestingly, using psyco.full() produced these results:
1 million tuple swaps in 3.06005958858 seconds.
1 million temp swaps in 3.01621882111 seconds.
1 million xor swaps in 3.03376686143 seconds.

The xorswap function is not much use for anything other than numbers,
however, I would have expected a psyco'ed xorSwap to much faster than
the alternatives!

Sw.

Gandalf
Guest
Posts: n/a

 04-21-2004
Josiah Carlson wrote:

>> The xorswap function is not much use for anything other than numbers,
>> however, I would have expected a psyco'ed xorSwap to much faster than
>> the alternatives!

>
>
> You are testing function calling overhead. Try the below...
>
> import time
>
> def tupleswap(a,b):
> t = time.time()
> for i in xrange(100000):
> a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;
> a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;
> print "1 million tuple swaps in", time.time()-t, "seconds."

AFAIK, this seems to be 10 thousand instead of 1 million (and 10 mil
the psyco test).

G

Josiah Carlson
Guest
Posts: n/a

 04-21-2004
>>> The xorswap function is not much use for anything other than numbers,
>>> however, I would have expected a psyco'ed xorSwap to much faster than
>>> the alternatives!

>>
>>
>>
>> You are testing function calling overhead. Try the below...
>>
>> import time
>>
>> def tupleswap(a,b):
>> t = time.time()
>> for i in xrange(100000):
>> a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;
>> a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;a,b=b,a;
>> print "1 million tuple swaps in", time.time()-t, "seconds."

>
>
> AFAIK, this seems to be 10 thousand instead of 1 million (and 10 mil
> instead of 100 mil for
> the psyco test).

You need to check your eyes. In tupleswap, the xrange is 100,000, and
the body of the for loop contains 10 tuple swaps, where 'a,b=b,a;' is
one tuple swap.

Similarly for tempswap, the xrange is 100,000, and the body of the for
loop contains 10 'swaps using a temporary variable', where
't=a;a=b;b=a;' is considered a single 'swap' of the a and b values,
using 't' as a temporary value.

This is also equivalent to the xor swap, where 'a^=b;b^=a;a^=b;' is a
single xor swap, 10 in the body, etc.

It should be trivial (by simple multiplication) that each conceptual
swap is done 1 million times. For the big psyco test, I used a bigger
xrange (100 times bigger in fact), which puts it at 100 million.

- Josiah

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Niels Dekker (no reply address) C++ 4 07-20-2006 08:44 PM hadAnet ASP .Net 2 12-28-2005 06:56 PM Goldneye MCSE 20 10-11-2005 10:10 PM Bill F Cisco 5 11-03-2004 03:45 AM Chaos Master Firefox 0 07-10-2004 05:00 AM