# RE: No built-in swap function?

Discussion in 'Python' started by Simon Wittber, Apr 21, 2004.

1. ### Simon WittberGuest

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.

Simon Wittber, Apr 21, 2004

2. ### GandalfGuest

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

Gandalf, Apr 21, 2004

3. ### Josiah CarlsonGuest

>>> 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.