Skip to content

Instantly share code, notes, and snippets.

@acdimalev
Created December 14, 2021 01:06
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 acdimalev/dc9d3adf06b06f17f13f856ed5496857 to your computer and use it in GitHub Desktop.
Save acdimalev/dc9d3adf06b06f17f13f856ed5496857 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# interactive log-log profiling visualizer with scales I personally find useful for microprocessor-scale optimizations
# usage: ./profile <depth> <filename>
#
# "depth" is basically the height of the graph
# it controlls how many samples are stored at any given time
# legend for reading the output
#
# - ---------- ---------- -
# ^ ^ ^ ^
# | | | '- more than a second
# | | |
# | | '- milliseconds
# | |
# | '- microseconds
# |
# '- less than a microsecond
import sys
from subprocess import PIPE, Popen
def render(depth, buckets):
document = ''
for y in reversed(range(0, depth, 2)):
for x in range(25):
# horizontal gaps
if 0 != (x + 10) % 11:
i = x - (x + 10) // 11
document += [' ', '.', ':'][max(0, min(2, buckets[i] - y))]
else:
document += ' '
document += '\n'
document += '- ---------- ---------- -'
return document
def main(depth, filename):
height = depth // 2 + 1
buflen = 2 ** depth
proc = Popen(
['tail', '-fn', str(buflen), '--', filename],
stdout=PIPE, text=True,
)
buffer = []
try:
# overprint
print('\n' * height)
for line in proc.stdout:
value = int(line).bit_length()
buffer = buffer[1-buflen:] + [value]
buckets = [0] * 22
for x in buffer:
buckets[min(21, x)] += 1
buckets = [int.bit_length(x) for x in buckets]
# overprint
print(f'\x1b[{1 + height}A')
print(render(depth, buckets))
except KeyboardInterrupt:
print('\r', end='')
finally:
proc.kill()
proc.wait()
(_, depth, filename) = sys.argv
main(int(depth), filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment