Advertisement
MrPolywhirl

MemEval.py

Oct 8th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import operator
  4. import time
  5.  
  6. KB = 1024
  7. MB = KB**2
  8. GB = MB*KB
  9.  
  10. INT_SIZE = 4;
  11.  
  12. ARRAY_MIN = KB
  13. ARRAY_MAX = (KB*4)**2
  14.  
  15. x = [0]*ARRAY_MAX
  16.  
  17. def step_range(start, end, step, step_func, compare_func):
  18.     while compare_func(start, end):
  19.         yield start
  20.         start = step_func(start, step)
  21.  
  22. def getSeconds():
  23.     return time.time()
  24.    
  25. def label(i):
  26.     if i < 1e3:
  27.         print '%1dB,'%(i),
  28.     elif i < 1e6:
  29.         print '%1dK,'%(i / KB),
  30.     elif i < 1e9:
  31.         print '%1dM,'%(i / MB),
  32.     else:
  33.         print '%1dG,'%(i / GB),
  34.    
  35. def main():
  36.     print ' ,',
  37.     for stride in step_range(1, ARRAY_MAX / 2, 2, operator.mul, operator.le):  
  38.         label(int(stride*INT_SIZE))
  39.     print
  40.    
  41.     # Main loop for each configuration
  42.     for csize in step_range(ARRAY_MIN, ARRAY_MAX, 2, operator.mul, operator.le):
  43.         label(int(csize * INT_SIZE)) #Print cache size for this loop
  44.         for stride in step_range(1, csize / 2, 2, operator.mul, operator.le):
  45.             # Layout path of memory references in array
  46.             for index in step_range(0, csize, stride, operator.add, operator.lt):
  47.                 x[index] = index + stride
  48.             x[index - stride] = 0
  49.            
  50.             ###print '[Stride='+str(stride)+']'
  51.             ###print '',
  52.            
  53.             # Wait for timer to roll over
  54.             lastsec = getSeconds()
  55.             while True:
  56.                 sec0 = getSeconds()
  57.                 if long(sec0) != long(lastsec):
  58.                     break
  59.            
  60.             # Walk through path in array for twenty seconds
  61.             # This gives 5% accuracy with second resolution
  62.             steps = 0.0; # Number of steps taken
  63.             nextstep = 0; # Start at beginning of path
  64.             sec0 = getSeconds() # Start timer
  65.             while True: # Repeat for 20 seconds
  66.                 for i in step_range(stride, 0, 1, operator.sub, operator.ne): # Keep sample size
  67.                     nextstep = 0
  68.                     while True:
  69.                         nextstep = x[nextstep]
  70.                         if nextstep == 0:
  71.                             break
  72.                 steps += 1
  73.                 sec1 = getSeconds()
  74.                 if (sec1 - sec0) >= 20.0:
  75.                     break
  76.             sec = sec1 - sec0;
  77.            
  78.             # Repeat empty loop to loop subtracted overhead
  79.             tsteps = 0.0; # used to match no. while iterations
  80.             sec0 = getSeconds() # Start timer
  81.             while True:
  82.                 for i in step_range(stride, 0, 1, operator.sub, operator.ne): # Keep sample size
  83.                     index = 0;
  84.                     while True:
  85.                         index += stride
  86.                         if index >= csize:
  87.                             break
  88.                 tsteps += 1
  89.                 sec1 = getSeconds()
  90.                 if tsteps >= steps:
  91.                     break
  92.             sec -= (sec1 - sec0)
  93.             loadtime = (sec * 1e9) / (steps * csize)
  94.            
  95.             # Write out results in CSV format
  96.             val = 0.1 if loadtime < 0.1 else loadtime
  97.             print '%4.1f,'%(val),
  98.         print
  99.  
  100. if __name__ == '__main__':
  101.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement