gruns / icecream

🍦 Never use print() to debug again.
MIT License
8.9k stars 183 forks source link

Unable to print timings of ic calls? #131

Open marksmayo opened 1 year ago

marksmayo commented 1 year ago

After seeing the issue about ic being slower, I thought I'd add a quick test:

def testPerformance(self): from timeit import timeit print(timeit('print("a")', number=1000)) print(timeit('ic("a")', 'from icecream import ic', number=1000))

however, the output with

pytest -k "Performance" -rP

gives me:

======================================================= PASSES ======================================================== ____ TestIceCream.testPerformance _____ ------------------------------------------------ Captured stdout call ------------------------------------------------- a a a a a a a a a a 0.0005997000262141228 0.022074999986216426 ------------------------------------------------ Captured stderr call ------------------------------------------------- ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ic| Error: Failed to access the underlying source code for analysis. Was ic() invoked in a REPL (e.g. from the command line), a frozen application (e.g. packaged with PyInstaller), or did the underlying source code change during execution? ========================================== 1 passed, 37 deselected / 1 selected

Which seems to indicate that I'm unable to call ic from within a timeit call? Wondering what I might be doing incorrectly?

alexmojaki commented 1 year ago

timeit doesn't make the source code of the statement available, so ic can't access it. If you call timeit('1/0') you can see that the source code 1/0 is missing from the traceback:

Traceback (most recent call last):
  File "foo.py", line 2, in <module>
    timeit.timeit('1/0')
  File "timeit.py", line 234, in timeit
    return Timer(stmt, setup, timer, globals).timeit(number)
  File "timeit.py", line 178, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
ZeroDivisionError: division by zero