Skip to content

Instantly share code, notes, and snippets.

@romuald
Created April 30, 2015 16:05
Show Gist options
  • Save romuald/0346c76cfbbbceb3e4d1 to your computer and use it in GitHub Desktop.
Save romuald/0346c76cfbbbceb3e4d1 to your computer and use it in GitHub Desktop.
Sub-millisecond profiling for python pstats
# -*- coding: utf8 -*-
"""
A simple monkeypath for the pstats module to be able to see sub-millisecond timings
If display value is 0.000, display the time in microseconds
"""
import pstats
def f8(x):
ret = "%8.3f" % x
if ret != ' 0.000':
return ret
return "%6dµs" % (x * 10000000)
pstats.f8 = f8
"""
Sample output example:
ncalls tottime percall cumtime percall filename:lineno(function)
9827 0.007 7µs 0.007 7µs {method 'get' of 'dict' objects}
3427 0.009 25µs 0.017 49µs {map}
10701 0.006 5µs 0.006 5µs {method 'lower' of 'str' objects}
31410 0.045 14µs 0.045 14µs {method 'split' of 'str' objects}
10023 0.006 6µs 0.006 6µs {_weakref.proxy}
31801 0.009 2µs 0.009 2µs {len}
3892 0.003 7µs 0.003 7µs {method 'extend' of 'list' objects}
1397 0.001 8µs 0.001 8µs {method 'replace' of 'str' objects}
10488 0.010 9µs 0.010 9µs {method 'string_literal' of '_mysql.connection' objects}
10702 2.620 2447µs 2.620 2447µs {method 'readline' of 'file' objects}
10023 0.004 3µs 0.004 3µs {method 'info' of '_mysql.connection' objects}
10023 0.005 5µs 0.005 5µs {method 'insert_id' of '_mysql.connection' objects}
14327 0.022 15µs 0.022 15µs {method 'rstrip' of 'str' objects}
""""
@sakurai-youhei
Copy link

Thanks for your sharing nice information!

@sub-zero7
Copy link

Thanks for the code, but it seems that you have one excess zero in the multiplier "10000000".

@pmorerio
Copy link

pmorerio commented Oct 28, 2022

Thanks for the code, but it seems that you have one excess zero in the multiplier "10000000".

In fact 2447µs would mean 0.002 seconds, which is not sub-millisecond

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment