With: IPython and it's magic function %timeit
So let me show you some examples with benchmarks. The numbers are not important. What is important is how easily you can get some speed improvements.# SLOW how_many = 0 for element in BIG_LIST: how_many += 1 print how_many
# FAST print len(BIG_LIST)
For 1 000 000 elements it's 26.5 ms vs 96.7 ns 274 000 times faster actually
Let's start with something simple. Let's say you want to count the number of elements in a list. You can easily write you own function with just a for loop that increments a variable. There is nothing wrong with this code, except that it's slow. You can achieve the same result using the built-in len() function. As you can see, the difference is quite huge. So, try to use the built-in functions when you can. There is just a few of them, so it's very easy to remember the most useful ones.