Advertisement
Guest User

compare (pool)

a guest
Nov 18th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. from time import time
  2. import multiprocessing
  3. from multiprocessing import Pool
  4.  
  5. # Initial params
  6. piece_size = 16 # KB
  7. # cores in system
  8. multiplier = 4
  9. errors = 0
  10.  
  11. starttime = time()
  12.  
  13. # 2 equal by size, but different by content files
  14. file1 = open('etalon.mkv', 'rb')
  15. file2 = open('startplus.mkv', 'rb')
  16.  
  17. # get byte piece of 2 files, and compare them
  18. def check(r1, r2, i):
  19.     global errors
  20.     if r1[i:i + piece_size] != r2[i:i + piece_size]:
  21.         errors += 1
  22.     return
  23.  
  24. if __name__ == '__main__':
  25.     # 4 process threads (opt for 4 core cpu)
  26.     pool = Pool(processes = multiplier)
  27.    
  28.     r1 = file1.read(piece_size * multiplier)
  29.     r2 = file2.read(piece_size * multiplier)
  30.    
  31.     while r1:
  32.         for i in range(0, piece_size * multiplier, piece_size):
  33.             pool.apply(check, [r1,r2,i])  
  34.         r1 = file1.read(piece_size)
  35.         r2 = file2.read(piece_size)
  36.    
  37.     print('errors:', errors)
  38.     print('Running time: ', time() - starttime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement