Re:How copying an int made my code 11 times faster
by Cyphase on Monday February 20, 2017 @03:20AM
(#13685433)
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).
Re:How copying an int made my code 11 times faster
by MooMooMilkParty on Monday February 20, 2017 @05:12AM
(#13686048)
Good call on going pure python. To take this a bit further I made your changes and used numba with @jit(nopython=True, cache=True), for some interesting results. If I do include the sorting into the timing:
Unsorted execution time: 0.2175428867340088 seconds
Sorted execution time: 1.133354663848877 seconds
And if I don't: Unsorted execution time: 0.21171283721923828 seconds
Sorted execution time: 0.08376479148864746 seconds
Re:How copying an int made my code 11 times faster
by rasz_pl on Monday February 20, 2017 @07:23AM
(#13686665)
Using python for micro benchmarks usually doesnt work. The mere fact you use python says you dont care about performance, but convenience.
Just for fun add "randomvariable = 1"(you know, 1 one cycle machine op) in a tight loop and watch tenths of a second added to your "benchmark".
Re:How copying an int made my code 11 times faster
by Cyphase on Monday February 20, 2017 @04:27PM
(#13691074)
I'm not quite sure what you mean by "using python for micro benchmarks usually doesn't work". To the extent that "microbenchmarking" is useful at all, then sure it works. There are just different considerations from other languages, and it depends on which Python implementation and libraries you're using.
Also, while I'll grant you that using Python implies that convenience, programmer time, and/or development speed is a higher priority than performance, that doesn't at all mean that people who use Python "don't care about performance".
Edit: After running with a wider range of parameters, it seems that the difference is always roughly the same order of magnitude. To investigate further, I included the sort into the second timing to double check and for 3276800 elements it's still a bit faster overall when you sort the array.