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

apart from the normal program output, the above script outputs the following stats

         210 function calls in 0.002 seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       20    0.001    0.000    0.001    0.000 {built-in method print}
      120    0.000    0.000    0.000    0.000 {built-in method len}
        4    0.000    0.000    0.000    0.000 omega.py:13(shuffle)
       64    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.002    0.002 omega.py:24(sim)


@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