Efficient Python part 2: Tools for Evaluating Your Code
Welcome to the second part out of four in the series on how to write more efficient python code. In the first part, we checked the very basics: Pythonic code, built-in functions, and just a bit on how numpy can improve call;ation speed. As a recap, the series comprehends:
- Efficient Python Part 1: Start with the Basics
- Efficient Python Part 2: Tools for Evaluating Your Code
- Efficient Python Part 3: Increasing code performance
- Efficient Python Part 4: Optimisation for Pandas
We are going to start with some basic and then we are looking for extra library that do the magic, we gonna use what python already offer to us. Once again, thanks datacamp for teaching some of those tricks!
Examining runtime
Last post we learned how to optimize a bit the code with some basics. Now, we will be able to check if the code is faster or not, by timing it. Your first option is:
# pass the magic command before the code to measureimport numpy as np
%timeit rand_nums = np.random.rand(1000)
- It will return the time to perform the action. TRY IT OUT!
- Comparing run time allow us to pick the fastest code