RE: No built-in swap function?

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

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


    And received the following output:
    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
    #1
    1. Advertising

  2. Simon Wittber

    Gandalf Guest

    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
    instead of 100 mil for
    the psyco test).

    G
    Gandalf, Apr 21, 2004
    #2
    1. Advertising

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

    Again, check your eyes.

    - Josiah
    Josiah Carlson, Apr 21, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. OSHIMA

    swap() function without tmp

    OSHIMA, May 14, 2004, in forum: C Programming
    Replies:
    30
    Views:
    1,258
    Dan Pop
    May 21, 2004
  2. Simon Wittber

    RE: No built-in swap function?

    Simon Wittber, Apr 21, 2004, in forum: Python
    Replies:
    1
    Views:
    427
    David Fraser
    Apr 21, 2004
  3. Replies:
    2
    Views:
    271
  4. Niels Dekker (no reply address)

    What swap is called when using std::swap?

    Niels Dekker (no reply address), Jul 19, 2006, in forum: C++
    Replies:
    4
    Views:
    989
    Niels Dekker (no reply address)
    Jul 20, 2006
  5. Jess
    Replies:
    28
    Views:
    754
    Daniel T.
    Jun 18, 2007
Loading...

Share This Page