question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

%%timeit could use local scope.

See original GitHub issue

You can’t use timeit in a function (which is useful to test variable inputs of several function doing the same things, and plot graphs.

I believe a well placed @needs_local_scope and modification to timeit should allow that.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
Carreaucommented, Jun 8, 2017

I was likely refering to the line magic %timeit not the cell magic. I tried to make a %timeit, in a function in a for loop. For example:

def fib(n):
    return 1 if n < 2 else fib(n-1)+fib(n-2)

def timefib(P):
    t = []
    for i in range(P):
        res = %timeit -o fib(i)
        t.append(res)
    return t

And this timeit would need local scope.

I believe that what I was referring to and inadvertantly have put 2 % sign.

0reactions
takluyvercommented, Jun 12, 2017

Yes, you are right. The genuine global var j is getting overwritten.

That’s unfortunate. I would have been inclined to hold off from merging the PR in that case. Hopefully not too many people will hit it, though, because it only affects using %timeit inside a function, which I think is fairly unusual.

But, I have got another way of solving this, How about nested calls to exec(). Inner call will be made from inner() and we could pass local_ns to inner() first and then exec() will use them to execute stmt.

That’s a decent idea, but I think we probably shouldn’t pursue it. Timeit goes to some lengths to eliminate as much code as possible between the time measurements and the code to execute, so that the timings are as accurate as possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local variables in Python timeit setup - Stack Overflow
I would like to use timeit. timeit with variables that are within the scope it is in, for example: def func(): s1 =...
Read more >
Python: Scoping variables to use with timeit - Mark Needham
I've been playing around with Python's timeit library to help benchmark some Neo4j cypher queries but I ran into some problems when trying ......
Read more >
How To Use The TimeIt Module in python - Twise Random
The timeIt module is used to test how fast our code is . We can use it to compare different functions or classes...
Read more >
timeit — Measure execution time of small code snippets ...
Source code: Lib/timeit.py This module provides a simple way to time small ... The following example shows how the Command-Line Interface can be...
Read more >
Python Timeit Module (With Examples) - Pylenin
Approach 2 - Use a list comprehension · It ignores the background processes running on your system, which might have an impact on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found