Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import dis
- def one(filepath):
- with open(filepath, 'r') as fp:
- line = fp.readline()
- count = 1
- while line:
- # do processing
- count +=1
- line = fp.readline()
- def two(filepath):
- with open(filepath, 'r') as fp:
- for i, line in enumerate(fp):
- # do processing
- pass
- print("function one")
- print("- - - - - - - - - - - ")
- dis.dis(one)
- print("function two")
- print("- - - - - - - - - - - ")
- dis.dis(two)
- here is the python bytecode of the respective loops
- (without the regex 'work' which should be the same)
- function one
- >> 26 LOAD_FAST 2 (line)
- 28 POP_JUMP_IF_FALSE 48
- # do processing is here
- 9 30 LOAD_FAST 3 (count)
- 32 LOAD_CONST 2 (1)
- 34 INPLACE_ADD
- 36 STORE_FAST 3 (count)
- 10 38 LOAD_FAST 1 (fp)
- 40 LOAD_ATTR 1 (readline)
- 42 CALL_FUNCTION 0
- 44 STORE_FAST 2 (line)
- 46 JUMP_ABSOLUTE 26
- function two
- >> 22 FOR_ITER 8 (to 32)
- 24 UNPACK_SEQUENCE 2
- 26 STORE_FAST 2 (i)
- 28 STORE_FAST 3 (line)
- # do processing is here
- 16 30 JUMP_ABSOLUTE 22
Advertisement
Add Comment
Please, Sign In to add comment