Skip to content

Instantly share code, notes, and snippets.

@matthewrmshin
Created January 28, 2021 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewrmshin/b8378cb72662700002702a4d93c7eaba to your computer and use it in GitHub Desktop.
Save matthewrmshin/b8378cb72662700002702a4d93c7eaba to your computer and use it in GitHub Desktop.
Python cProfile script
#!/bin/bash
set -euo pipefail
finally() {
trap '' ERR
trap '' EXIT
if [[ -n "${WORKD:-}" ]]; then
rm -fr "${WORKD}"
fi
}
CMD="$1"
shift 1
WORKD="$(mktemp -d)"
trap 'finally' ERR
trap 'finally' EXIT
/usr/bin/time -v \
python3 -m 'cProfile' -o "${WORKD}/profile.out" "$(command -v "$CMD")" "$@"
echo -e "sort cumtime\nstats" \
| python3 -m 'pstats' "${WORKD}/profile.out" \
| "${PAGER:-cat}"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment