SHARE
TWEET

Untitled

a guest Jun 20th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with open(file, "rb") as infile:
  2.      data_arr = []
  3.      start = time()
  4.      while True:
  5.          data = infile.read(6)
  6.          if not data: break
  7.          data_arr.append(data)
  8.      
  9. import timeit
  10.  
  11.  
  12. def read_original(filename):
  13.     with open(filename, "rb") as infile:
  14.         data_arr = []
  15.         while True:
  16.             data = infile.read(6)
  17.             if not data:
  18.                 break
  19.             data_arr.append(data)
  20.     return data_arr
  21.  
  22.  
  23. # the bigger the file, the more likely this is to cause python to crash
  24. def read_better(filename):
  25.     with open(filename, "rb") as infile:
  26.         # read everything into memory at once
  27.         data = infile.readlines()
  28.         # separate string into 6-byte chunks
  29.         data_arr = [data[i:i+6] for i in range(0, len(data), 6)]
  30.     return data_arr
  31.  
  32. # no faster than the original, but allows you to work on each piece without loading the whole into memory
  33. def read_iter(filename):
  34.     with open(filename, "rb") as infile:
  35.         data = infile.read(6)
  36.         while data:
  37.             yield data
  38.             data = infile.read(6)
  39.  
  40.  
  41. def main():
  42.     # 93.8688215 s
  43.     tm = timeit.timeit(stmt="read_original('test/oraociei12.dll')", setup="from __main__ import read_original", number=10)
  44.     print(tm)
  45.     # 8.579144 s
  46.     tm = timeit.timeit(stmt="read_better('test/oraociei12.dll')", setup="from __main__ import read_better", number=10)
  47.     print(tm)
  48.     # 103.0508528 s
  49.     tm = timeit.timeit(stmt="[x for x in read_iter('test/oraociei12.dll')]", setup="from __main__ import read_iter", number=10)
  50.     print(tm)
  51.  
  52. if __name__ == '__main__':
  53.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top