Fix My Tutorial
Report

How to Time Small Pieces of Python Code with timeit

Open the original tutorial
Fixed Python 3.12 · checked 17 Oct 2026 · model cost $0.025

The tutorial originally produced a SyntaxError due to Python 2 print syntax and an incorrect use of run_code for a shell command in step s7. After migrating the script to Python 3 print functions and changing step s7 to execute as a shell command, the code runs without error under Python 3.12. No specific version removals were cited in the sources.

1 change to make it work today

Create timeit_demo.py

s6 · write_file migrate

Fix Python 2 print statement to Python 3 function call to avoid SyntaxError.

@@ -8,3 +8,3 @@
import timeit
setup = "from __main__ import my_function"
- print timeit.timeit("my_function()", setup=setup)
+ print(timeit.timeit("my_function()", setup=setup))

Sources

Tell the author

Send them what broke and the fix, so the next reader doesn't hit the same error.

Badge

Add it to the tutorial so readers know when it was last checked.

Fix My Tutorial badge
Markdown
[![Fix My Tutorial](https://fix-my-tutorial.onrender.com/badge/ri-ZyxfTtnp.svg)](https://fix-my-tutorial.onrender.com/r/ri-ZyxfTtnp)
HTML
<a href="https://fix-my-tutorial.onrender.com/r/ri-ZyxfTtnp"><img src="https://fix-my-tutorial.onrender.com/badge/ri-ZyxfTtnp.svg" alt="Fix My Tutorial"></a>