Advertisement
Dmitrey15

Untitled

Mar 7th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. # Notebook Intel Atom 1.6 GHz:
  2. # CPython 2.7.5: 1.6 sec
  3. # PyPy 2.0.2: 0.6 sec
  4.  
  5. def process():
  6.     with open('src.csv') as f:
  7.         content = f.readlines()
  8.     D = {}
  9.  
  10.     for line in content:
  11.         num, str = line.split(',', 1)
  12.         D[num] = str
  13.        
  14.     maxLenght = max(len(key) for key in D.keys())
  15.  
  16.    
  17.     ins = open('data.dat', 'r')
  18.  
  19.     r, counter = [], 0
  20.     for line in ins:
  21.         for j in range(maxLenght, 0, -1):
  22. #            PyPy cannot optimize code with variable changing its type,
  23. #           for example,  if we uncomment these 2 lines
  24. #            a = 'asdf'
  25. #            a = 4
  26. #            then PyPy speed will drop to CPython speed
  27.             Tmp = D.get(line[:j], None)
  28.             if Tmp is None: continue
  29.             r.append(line[:-1] +' => '+Tmp)
  30.             break
  31.        
  32.     ins.close()
  33.     res_file = open('output2.txt', 'w')
  34.     res_file.writelines(r)
  35.     res_file.close()
  36.  
  37.    
  38. if __name__ == '__main__':
  39.     from time import time
  40.     t = time()
  41.     process()
  42.     print('time elapsed:', time()-t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement