Guest User

Untitled

a guest
Oct 17th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import dis
  2.  
  3. def one(filepath):
  4. with open(filepath, 'r') as fp:
  5. line = fp.readline()
  6. count = 1
  7. while line:
  8. # do processing
  9. count +=1
  10. line = fp.readline()
  11.  
  12. def two(filepath):
  13. with open(filepath, 'r') as fp:
  14. for i, line in enumerate(fp):
  15. # do processing
  16. pass
  17.  
  18.  
  19. print("function one")
  20. print("- - - - - - - - - - - ")
  21. dis.dis(one)
  22.  
  23. print("function two")
  24. print("- - - - - - - - - - - ")
  25. dis.dis(two)
  26.  
  27.  
  28. here is the python bytecode of the respective loops
  29. (without the regex 'work' which should be the same)
  30.  
  31. function one
  32. >> 26 LOAD_FAST 2 (line)
  33. 28 POP_JUMP_IF_FALSE 48
  34. # do processing is here
  35. 9 30 LOAD_FAST 3 (count)
  36. 32 LOAD_CONST 2 (1)
  37. 34 INPLACE_ADD
  38. 36 STORE_FAST 3 (count)
  39.  
  40. 10 38 LOAD_FAST 1 (fp)
  41. 40 LOAD_ATTR 1 (readline)
  42. 42 CALL_FUNCTION 0
  43. 44 STORE_FAST 2 (line)
  44. 46 JUMP_ABSOLUTE 26
  45.  
  46. function two
  47. >> 22 FOR_ITER 8 (to 32)
  48. 24 UNPACK_SEQUENCE 2
  49. 26 STORE_FAST 2 (i)
  50. 28 STORE_FAST 3 (line)
  51. # do processing is here
  52. 16 30 JUMP_ABSOLUTE 22
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment