Skip to content

Instantly share code, notes, and snippets.

@ishankhare07
Created March 25, 2015 13:59
Show Gist options
  • Save ishankhare07/c8c77c02cfb0c7cb5469 to your computer and use it in GitHub Desktop.
Save ishankhare07/c8c77c02cfb0c7cb5469 to your computer and use it in GitHub Desktop.
cProfile based python profiling an omega network perfect shuffle simulation
#simulate omega network
import math
import sys
import cProfile, pstats
class simulate:
def __init__(self):
self.a = range(int(input('Enter number of inputs:')))
self.b = self.a[:]
self.count = int(math.log(len(self.a),2))
self.var = 1
def shuffle(self,list):
print('call', str(self.var))
self.var += 1
return_list = []
for x in list:
if x == len(list)-1 or x == 0:
return_list.append(x)
else:
return_list.append((x<<1) % (len(self.a) - 1))
return return_list
def sim(self):
while(self.count):
self.a = self.shuffle(self.a)
self.count -= 1
for x,y in zip(self.b,self.a):
print(str(x), '\t=>\t', str(y))
if __name__ == '__main__':
sim = simulate()
profiler = cProfile.Profile()
profiler.enable() #start collecting profiling data
sim.sim()
profiler.disable() #stop collecting profile data
stats = pstats.Stats(profiler) #generate stats
stats.print_stats() #print stats
@ishankhare07
Copy link
Author

more features like profiling a function call with parameters etc. are documented here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment