Efficient Python part 2: Tools for Evaluating Your Code

Cássio Bolba
4 min readAug 12, 2022

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:

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

--

--

Cássio Bolba
Cássio Bolba

Written by Cássio Bolba

Senior Data Engineer | Udemy Teacher | Expat in Germany | Mentor -> https://linktr.ee/cassiobolba

Responses (2)