|
|
|
|
|
by Cyphase
3407 days ago
|
|
I tried this on my machine, then tried a pure Python version; I only changed three lines, to: import random
...
arr = [random.randint(0, 256) for x in range(n)]
...
arr = sorted(arr)
Here are my times: $ time python2.7 hackernews_13682929.py
Unsorted execution time: 4.33348608017 seconds
Sorted execution time: 4.09405398369 seconds
$ time python3.5 hackernews_13682929.py
Unsorted execution time: 4.4200146198272705 seconds
Sorted execution time: 4.188237905502319 seconds
$ time python2.7 hackernews_13682929_purepython.py
Unsorted execution time: 0.981621026993 seconds
Sorted execution time: 0.832424879074 seconds
$ time python3.5 hackernews_13682929_purepython.py
Unsorted execution time: 1.3005650043487549 seconds
Sorted execution time: 1.157465934753418 seconds
$ time pypy hackernews_13682929_purepython.py
Unsorted execution time: 0.239459037781 seconds
Sorted execution time: 0.0910339355469 seconds
As you can see, the pure Python version is faster than the Numpy version, and also has a larger margin between unsorted and sorted. PyPy is of course faster than both, and also has an even greater margin between unsorted and sorted (2.63x faster). |
|