Tutorial by Examples

Profiling string concatanation: In [1]: import string In [2]: %%timeit s=""; long_list=list(string.ascii_letters)*50 ....: for substring in long_list: ....: s+=substring ....: 1000 loops, best of 3: 570 us per loop In [3]: %%timeit long_list=list(string.ascii_letters)*50...
Profiling repetition of elements in an array >>> import timeit >>> timeit.timeit('list(itertools.repeat("a", 100))', 'import itertools', number = 10000000) 10.997665435877963 >>> timeit.timeit('["a"]*100', number = 10000000) 7.118789926862576
Profiling concatanation of numbers python -m timeit "'-'.join(str(n) for n in range(100))" 10000 loops, best of 3: 29.2 usec per loop python -m timeit "'-'.join(map(str,range(100)))" 100000 loops, best of 3: 19.4 usec per loop
The source code with @profile directive before the function we want to profile: import requests @profile def slow_func(): s = requests.session() html=s.get("https://en.wikipedia.org/").text sum([pow(ord(x),3.1) for x in list(html)]) for i in range(50): s...
Python includes a profiler called cProfile. This is generally preferred over using timeit. It breaks down your entire script and for each method in your script it tells you: ncalls: The number of times a method was called tottime: Total time spent in the given function (excluding time made in c...

Page 1 of 1