import profile def foo(bar=None): print("bar = %s" % bar) profile.runctx("foo", globals(), {'bar': 'good'}) profile.run("foo('great')") OUTPUT: python2 test.py 3 function calls in 0.023 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.023 0.023 0.023 0.023 :0(setprofile) 1 0.000 0.000 0.000 0.000 :1() 1 0.000 0.000 0.023 0.023 profile:0(foo) 0 0.000 0.000 profile:0(profiler) bar = great 4 function calls in 0.003 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 0.000 0.000 0.003 0.003 :1() 1 0.000 0.000 0.003 0.003 profile:0(foo('great')) 0 0.000 0.000 profile:0(profiler) 1 0.003 0.003 0.003 0.003 test.py:3(foo)