Why is Pythons sorted() slower than copy, then .sort()

2024/10/9 21:20:29

Here is the code I ran:

import timeitprint timeit.Timer('''a = sorted(x)''', '''x = [(2, 'bla'), (4, 'boo'), (3, 4), (1, 2) , (0, 1), (4, 3), (2, 1) , (0, 0)]''').timeit(number = 1000)
print timeit.Timer('''a=x[:];a.sort()''', '''x = [(2, 'bla'), (4, 'boo'), (3, 4), (1, 2) , (0, 1), (4, 3), (2, 1) , (0, 0)]''').timeit(number = 1000)

and here are the results:

0.00259663215837
0.00207390190177

I would like to know why using .sort() is consistently faster than sorted() even though both are copying lists?

Note: I am running Python 2.7 on an 2.53Ghz i5 with Win7

Answer

The difference you are looking at is miniscule, and completely goes away for longer lists. Simply adding * 1000 to the definition of x gives the following results on my machine:

2.74775004387
2.7489669323

My best guess for the reason that sorted() was slightly slower for you is that sorted() needs to use some generic code that can copy any iterable to a list, while copying the list directly can make the assumption that the source is also a list. The sorting code used by CPython is actually the same for list.sort() and sorted(), so that's not what is causing the difference.

Edit: The source code of the current development version of sorted() does the moral equivalent of

a = list(x)
a.sort()

and indeed, using this code instead of your second version eliminates any significant speed differences for any list sizes.

https://en.xdnf.cn/q/69972.html

Related Q&A

How to efficiently unroll a matrix by value with numpy?

I have a matrix M with values 0 through N within it. Id like to unroll this matrix to create a new matrix A where each submatrix A[i, :, :] represents whether or not M == i.The solution below uses a lo…

Anaconda Python 3.6 -- pythonw and python supposed to be equivalent?

According to Python 3 documentation, python and pythonw should be equivalent for running GUI scripts as of 3.6With older versions of Python, there is one Mac OS X quirk that you need to be aware of: pr…

Good way of handling NoneType objects when printing in Python

How do I go about printin a NoneType object in Python?# score can be a NonType object logging.info("NEW_SCORE : "+score)Also why is that sometime I see a comma instead of the + above?

problems with easy_install pycrypto

Im trying install pycrypto on osx with easy_install and Im getting the following error:easy_install pycrypto Searching for pycrypto Reading http://pypi.python.org/simple/pycrypto/ Reading http://pycryp…

What is the most efficient way to do a sorted reduce in PySpark?

I am analyzing on-time performance records of US domestic flights from 2015. I need to group by tail number, and store a date sorted list of all the flights for each tail number in a database, to be re…

Interactive figure with OO Matplotlib

Using Matplotlib via the OO API is easy enough for a non-interactive backend:from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvasfrom matplotlib.figure import Figurefig = Figure(…

nose2 vs py.test with isolated processes

We have been using nosetest for running and collecting our unittests (which are all written as python unittests which we like). Things we like about nose:uses standard python unit tests (we like the st…

ValueError: Attempt to reuse RNNCell with a different variable scope than its first use

The following code fragmentimport tensorflow as tf from tensorflow.contrib import rnnhidden_size = 100 batch_size = 100 num_steps = 100 num_layers = 100 is_training = True keep_prob = 0.4input_da…

Convex Hull and SciPy

Im trying to use scipy (0.10.1) for a quick hack to visualize the convex hull.I can get the convex hull using the following code:vecs = [[-0.094218, 51.478927], [-0.09348, 51.479364], [-0.094218, 51.4…

Flask Confirm Action

Im creating a site using the Flask framework, and am implementing a confirmation page for (mainly administrative) actions; i.e. deleting a user.My current method (detailed below) works, but feels quite…