Should I use "if" or "try" (as a matter of speed)?

Wezzy wezzy at despammed.com
Sat Jul 9 15:15:09 EDT 2005


wittempj at hotmail.com <martin.witte at gmail.com> wrote:

> My shot would be to test it like this on your platform like this:
> 
> #!/usr/bin/env python
> import datetime, time
> t1 = datetime.datetime.now()
> for i in [str(x) for x in range(100)]:
>       if int(i) == i:
>               i + 1
> t2 = datetime.datetime.now()
> print t2 - t1
> for i in [str(x) for x in range(100)]:
>       try:
>               int(i) +1
>       except:
>               pass
> t3 = datetime.datetime.now()
> print t3 - t2
> 
> for me (on python 2.4.1 on Linux on a AMD Sempron 2200+) it gives:
> 0:00:00.000637
> 0:00:00.000823

PowerBook:~/Desktop wezzy$ python test.py 
0:00:00.001206
0:00:00.002092

Python 2.4.1 Pb15 with Tiger
-- 
Ciao
Fabio



More information about the Python-list mailing list