pythonipythontimeitmagic-function

Magic function timeit


I'm using the magic %%timeit function to get the time it takes to execute some code. The thing that bothers me is that when I run %%timeit, I don't get the results. For instace:

a=5
b=3

%%timeit
c = a + b

Now if I want to use c in the next cell, I get that c hasn't been defined.

print(c)
>>>NameError: name 'c' is not defined

Could you help me understand why this happens, why doesn't c get stored when the magic %%timeit function is used in that particular cell?


Solution

  • When you time code with %%timeit, the code you provide is executed within a separate namespace, and so its effects are not visible to your environment.