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

wittempj@hotmail.com martin.witte at gmail.com
Sat Jul 9 14:58:17 EDT 2005


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




More information about the Python-list mailing list