Skip to content

Instantly share code, notes, and snippets.

@ihnorton
Last active June 21, 2018 07:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihnorton/05f1dea38e596e75ba106855d490e66a to your computer and use it in GitHub Desktop.
Save ihnorton/05f1dea38e596e75ba106855d490e66a to your computer and use it in GitHub Desktop.
bare-bones uniq for cmake trace profiling
#!/usr/bin/python
# usage:
# > cmake --trace > foo
# > cmake_prof.py foo
# Return a sorted list of (hitcount, line) for lines in a given file
# dramatically faster than running `sort | uniq` on large files.
def file_line_hitcount(fname):
with open(fname) as f:
h = {}
for l in f.readlines(): h[l] = h.get(l, 0) + 1
rev_paired = [(h[key],key) for key in h.keys()]
return sorted(rev_paired, cmp=lambda x,y: y[0] - x[0])
if __name__ == '__main__' and not '__file__' in globals():
import sys
assert(len(sys.argv) == 2)
for c,l in file_line_hitcount(sys.argv[1]):
s1,s2 = l.strip().split(':')
print s1 + ": " + str(c) + " -- " + s2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment