Skip to content

Instantly share code, notes, and snippets.

@hvy
Created August 16, 2017 05:27
Show Gist options
  • Save hvy/9773a22e871e3db0f921d0f91d749c99 to your computer and use it in GitHub Desktop.
Save hvy/9773a22e871e3db0f921d0f91d749c99 to your computer and use it in GitHub Desktop.
cProfile from the cli with environ variables as output filenames similar to nvprof
# https://github.com/python/cpython/blob/master/Lib/profile.py
def _show(self, prof, filename, sort):
if filename is not None:
# NOTE(hvy): Replace environment variables similar to how
# nvprof does by e.g. replacing out_%q{MV2_COMM_WORLD_RANK} with
# out_1, out_2, etc. depending on the value of the variable
# MV2_COMM_WORLD_RANK
import re
if re.search(r'\%q{(.*?)\}', filename) is not None:
filename = re.sub(r'\%q{(.*?)\}',
lambda match: os.environ[match.group(1)],
filename)
prof.dump_stats(filename)
else:
prof.print_stats(sort)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment