Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Tested copy(), [:], and list():

  >>> t1 = timeit.Timer('copy.copy(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(100000)];')
  >>> t2 = timeit.Timer('orig[:]','import copy;import random;orig = [random.randint(0,255) for r in xrange(100000)];')
  >>> t3 = timeit.Timer('list(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(100000)];')

  >>> print t1.timeit(10000)/10000
  0.00036607401371
  >>> print t2.timeit(10000)/10000
  0.000416543197632
  >>> print t3.timeit(10000)/10000
  0.000372415995598
Probably not the world's most ideal testing here. I have no idea if/how Python caches, for instance. But slice notation continually comes out the slowest in this crummy example.


Not at my computer right now to whip up a simple benchmark(phone), but there is a pretty exhaustive benchmark here: http://stackoverflow.com/questions/2612802/how-to-clone-a-li...


With smaller lists, the results in my test change:

  >>> t1 = timeit.Timer('copy.copy(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(10)];')
  >>> t2 = timeit.Timer('orig[:]','import copy;import random;orig = [random.randint(0,255) for r in xrange(10)];')
  >>> t3 = timeit.Timer('list(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(10)];')

  >>> print t1.timeit(10000)
  0.0183310508728
  >>> print t2.timeit(10000)
  0.00397896766663
  >>> print t3.timeit(10000)
  0.00760293006897
Which is similar to those results.

This implies more setup cost for copy() and list(), but after that they're faster.


If not for noise, list() should always outperform copy() as copy() just calls list() internally (specifically type(l)(l)), and also incurs the cost of several interrupted function calls.

Also, the minor difference in slice vs. list() for large lists are likely platform dependent and highly sensitive to the details of branch prediction and cache.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: