Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nom3ad/7f3c9716efdc26d66ace809792f60b26 to your computer and use it in GitHub Desktop.
Save nom3ad/7f3c9716efdc26d66ace809792f60b26 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}
""""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment