Skip to content

Instantly share code, notes, and snippets.

@mfarrugi
Last active October 22, 2018 12:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfarrugi/e82850510673946cb7cf to your computer and use it in GitHub Desktop.
Save mfarrugi/e82850510673946cb7cf to your computer and use it in GitHub Desktop.
Benchmark shell scripts!
#/usr/bin/python
import sys
import re
lines = sys.argv[1]
ms_thresh = int(sys.argv[2])
lines = lines.split("\n")
lines = map(str.strip, lines)
events = ( (float(event[0]), event[1])
for event in (tuple(line.split(" :: ")) for line in lines) )
first = next(events)
start = first[0]
end = 0
prev = start
durations = []
for event in events:
time, label = event
duration = time - prev
prev = time
durations.append((duration, label))
end = time
total = end - start
durations.sort(key=lambda x: x[0], reverse=True)
for dur, lab in durations:
if dur * 1000 < ms_thresh:
break
print "{:4.1f} ms {:5.2f}% :: {}".format(dur * 1000, dur / total * 100, lab)
print "-" * 60
print "{:4.1f} ms :: {}".format(total * 1000, "Total Time")
rm -f ~/perf.txt
# Each call to mark took about 5ms on my machine, so it's not good for profiling anything slower.
function mark() {
echo "$(date +%s.%N) :: $1" >> ~/perf.txt
}
mark "Start"
# ... load stuff
mark "Loading"
# ... do stuff
mark "Done"
# Not sure why I couldn't pipe cat, but oh well.
python benchmark-sort.py "$(cat ~/perf.txt)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment