Title: Test Code Speed
Slug: test_code_speed
Summary: Test Code Speed in Python.
Date: 2016-01-23 12:00
Category: Python
Tags: Testing
Authors: Chris Albon
Interesting in learning more? Here are some good books on unit testing in Python: Python Testing: Beginner's Guide and Python Testing Cookbook.
In [1]:
import cProfile
In [2]:
def slow_function():
total = 0.0
for i, _ in enumerate(range(10000)):
for j, _ in enumerate(range(1, 10000)):
total += (i * j)
return total
In [3]:
cProfile.run('slow_function()', sort='time')
In [4]:
%%timeit
slow_function()