Reputation: 35
I'm using and try/except statement in python inside a very long loop. If the exception is raised, it should do nothing.
try :
*Some Code*
except :
pass
If i use this first proposition, the total time of calculation for the loop is about 10 minutes.
try :
*Some Code*
except :
None
If i use this second propositon, the total time of calculation for the loop is about 2 minutes.
Why is it so different, and why is the second one faster as, logically for me, pass is a better solution than None ?
The exact code is :
try:
indexes = peakutils.peak.interpolate(self.list_height, input_1, ind=indexes, width=self.gauss_width)
except:
None / Pass
I made several tests with both propisitons and it's always the same.
How time is calculated :
start = default_timer()
im.get_events() #The loop where the try/except statement appears
finish = default_timer()
print('ELAPSED TIME : %s'%(finish - start))
Upvotes: 0
Views: 582
Reputation: 5474
Given two functions:
def func_none():
return None
def func_pass():
pass
They are doing exactly the same thing, the proof using dis
:
>>> import dis
>>> dis.dis(func_none)
# 0 LOAD_CONST 0 (None)
# 3 RETURN_VALUE
>>> dis.dis(func_pass)
# 0 LOAD_CONST 0 (None)
# 3 RETURN_VALUE
Indeed, here is the definition of pass:
pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example:
Conclusion: pass
statement is as fast as None
in theory.
Edit: why it may be a slower time using None
instead of return None
(like in OP example).
Given two functions:
def func_none():
try:
1/0
except:
None
def func_pass():
try:
1/0
except:
pass
The difference using dis:
>>> import dis
>>> dis.dis(func_none)
# Skip to expect part
18 LOAD_CONST 0 (None)
21 POP_TOP
22 JUMP_FORWARD 1 (to 26)
25 END_FINALLY
26 LOAD_CONST 0 (None)
29 RETURN_VALUE
dis.dis(func_pass)
# Skip to expect part
18 JUMP_FORWARD 1 (to 22)
21 END_FINALLY
22 LOAD_CONST 0 (None)
25 RETURN_VALUE
So it is clear that using None
instead of pass
will add two extra operation LOAD_CONST
and POP_TOP
. It shouldnt add 8 minutes though, so I'm sure this isn't the real reason.
Upvotes: 3