inline exception handling in python

wheres pythonmonks wherespythonmonks at gmail.com
Thu Aug 12 16:18:37 EDT 2010


On Thu, Aug 12, 2010 at 2:57 PM, Thomas Jollans <thomas at jollybox.de> wrote:
> On Thursday 12 August 2010, it occurred to wheres pythonmonks to exclaim:
>> [I just hate function call overhead for this.]
>
> I think you've got your priorities wrong. If you want to avoid unnecessary
> overhead, avoid exceptions more than functions.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Well I suppose it matters depending on the nature of the data you are
looking at...  But small function calls tend to be the death of
interpreted languages...

>>> timeit.timeit("""
def f(y,i):
 try:
  return(y/(i%10))
 except:
  return(float("nan"))

for i in range(100):
 x = f(7,i)

""")
56.362180419240985

>>> timeit.timeit("""
for i in range(100):
 try:
  x = 7 / (i % 10)
 except:
  x = float("nan")
""")
34.588313601484742
>>>



More information about the Python-list mailing list