Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Change reversed() to [::-1] for speed #13
Interestingly, I just did a bench and reversed it much faster anyway with larger lists. 25x faster when using the example gist.
That benchmark is incorrect -
In : lst = ['x'] * 1000 In : %timeit lst[::-1] 100000 loops, best of 3: 4.37 µs per loop In : %timeit list(reversed(lst)) 100000 loops, best of 3: 8.27 µs per loop
In : s = 'x' * 1000 In : %timeit s[::-1] 1000000 loops, best of 3: 883 ns per loop In : %timeit ''.join(reversed(s)) 10000 loops, best of 3: 20.7 µs per loop
After a (very silly) discussion about this micro-optimization, http://chat.stackoverflow.com/transcript/message/29868921#29868921, it looks like it would be better to replace the whole thing with the following, which outperforms everything else on py2 and py3 and doesn't require compat imports.
import struct to_int64 = struct.Struct('>Q').pack def int_to_bytes(num): return to_int64(num).lstrip(b'\x00') timeit int_to_bytes(ts) 1000000 loops, best of 3: 398 ns per loop