fork download
  1. from time import clock
  2.  
  3. def test_return(n):
  4. def f(x):
  5. if x % 2:
  6. return None
  7. return x
  8. s = 0
  9. for x in xrange(n):
  10. r = f(x)
  11. if r is not None:
  12. s += r
  13. return s
  14.  
  15. def test_except(n):
  16. def f(x):
  17. if x % 2:
  18. raise StopIteration()
  19. return x
  20. s = 0
  21. for x in xrange(n):
  22. try:
  23. s += f(x)
  24. except StopIteration:
  25. pass
  26. return s
  27.  
  28. N = 1000000
  29.  
  30. t = clock()
  31. print test_return(N)
  32. print 'Return: ', clock() - t
  33.  
  34. t = clock()
  35. print test_except(N)
  36. print 'Raise: ', clock() - t
Success #stdin #stdout 1.41s 7900KB
stdin
Standard input is empty
stdout
249999500000
Return:  0.37
249999500000
Raise:  1.04