Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Last active September 27, 2019 16:32
Show Gist options
  • Save mgedmin/5005830 to your computer and use it in GitHub Desktop.
Save mgedmin/5005830 to your computer and use it in GitHub Desktop.
using timeit.timeit from pdb with access to a subset of local vars

= Problem =

timeit.timeit() doesn't let you pass local vars to the code to be timed.

= Solution =

(pdb) !import sys, timeit
(Pdb) !dv = (lambda sys=sys: (lambda **kw: setattr(sys.modules['__main__'], 'VARS', kw)))()
(Pdb) !tt = lambda s, sys=sys: sys.stdout.write('%.3g ms\n' % timeit.timeit(s, 'import sys; self = sys.modules["__main__"].SELF', number=10)/10.*1000)

= Example =

(Pdb) dv(self=self, any_other_name=any_other_name)
(Pdb) tt('any_other_name(self.someattribute)')
42.123 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment