Skip to content

Instantly share code, notes, and snippets.

@Krewn
Last active May 28, 2022 17:36
Show Gist options
  • Save Krewn/038021efbae79e5304644eb08d0ab9c9 to your computer and use it in GitHub Desktop.
Save Krewn/038021efbae79e5304644eb08d0ab9c9 to your computer and use it in GitHub Desktop.
Scatter plot of method calls performed vs. clock ticks.
import os
import time
import matplotlib.pyplot as plt
import numpy as np
import random
import mplcursors
def randHex(length,psudo=False):
if psudo:
ret = hex(random.randint(0,16**(length*2)))[2:]
return "0"*(length*2-len(ret))+ret
return "".join([hex(b)[2:] for b in os.urandom(length)])
def lap(l,verbose = False, psudo = False):
start = time.time()
r = randHex(l,psudo=psudo)
end = time.time()
if verbose:
print(r)
return(end-start)
def calibrate(psudo=False):
l = 32
trials = 10000
t = 0.
count = 0
for k in range(0,trials):
print("Calibrating:"+str(round(k*100/trials,1))+"%",end="\r")
inc = lap(l,psudo = psudo)
if inc:
count += 1
t += inc
return( t/count )
def lapSet(duration,psudo=False):
expected = calibrate(psudo=psudo)
lastLap = expected
laps = 0
print(expected)
zeroCount = 0
times = []
lapCounts = []
start = time.time()
elapsed = time.time()-start
lastMark = start
#now = lastMark
while elapsed < duration:
#print(elapsed,end="\r")
now = time.time()
if lastLap or lastMark != now:
if lastMark != now:
times.append(now-lastMark)
else:
times.append(lastLap)
lastMark = now
lapCounts.append(zeroCount)
zeroCount = 0
l = int(32*(lastLap/expected))
#print()
lastLap = lap(l,verbose=False,psudo=psudo)
else:
zeroCount+=1
l
astLap = lap(l,psudo=psudo)
#print(zeroCount,end="\r")
laps += 1
elapsed = time.time()-start
return {"times":times,"laps":lapCounts}
psudoData = lapSet(5,psudo = True)
urandomData = lapSet(5,psudo = False)
allPoints = list(zip(psudoData["times"],psudoData["laps"]))+list(zip(urandomData["times"],urandomData["laps"]))
counts = {}
for point in allPoints:
if point in counts.keys():
continue
else:
counts[point] = len([k for k in allPoints if k == point])
artist1 = plt.scatter(psudoData["times"], psudoData["laps"],color="blue",alpha=0.4)
artist2 = plt.scatter(urandomData["times"], urandomData["laps"],color="red",alpha=0.4)
plt.xlabel("Times")
plt.ylabel("Laps")
cursor = mplcursors.cursor(hover=mplcursors.HoverMode.Transient)
@cursor.connect("add")
def on_add(sel):
#print(type(sel.artist))
if sel.artist == artist1:
txt = str(counts[allPoints[sel.index]])+":"+str(allPoints[sel.index])
else:
txt = str(counts[allPoints[len(psudoData["times"])+sel.index]])+":"+str(allPoints[len(psudoData["times"])+sel.index])
sel.annotation.set(text=txt)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment