2 ms·
Python Performance Tips
- eesmith 7y agoIgnore this page! It's over 20 years old and nearly worthless. "Pyrex" is now Cython. "Psycho" provided an early basis for PyPy. cProfile is a C-based implementation of the pure-Python 'profile' module. Support has been dropped for the "hotshot" module. The 'sort' method takes a 'key' parameter which implements 'the so-called Schwartzian Transform' (decorate-sort-undecorate). There's no need to implement it manually. map() now returns an iterator instead of list, so the timings, as given, won't work. Given: python -mtimeit -s 'import random' -s 'data = [str(random.random()) for _ in range(10000)]' then '[x.upper() for x in data]' is significantly faster than 'z=[]' 'for x in data: z.append(x.upper())' (889 usec per loop instead of 1.22 msec per loop). Use collections.defaultdict to have an initial default value for missing dictionary entries. The following appears to be wrong: dict = {} nil = [] for s in list: dict[s] = nil as all dict values will share the same list object. The timings have changed so that there is no longer "a significant advantage in Python to adding a number to itself instead of multiplying it by two": % python -m timeit -s 'x = 47' 'x * 2' 10000000 loops, best of 5: 26.1 nsec per loop % python -m timeit -s 'x = 47' 'x << 1' 5000000 loops, best of 5: 44.3 nsec per loop % python -m timeit -s 'x = 47' 'x + x' 10000000 loops, best of 5: 25.5 nsec per loop