KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Consider the following functions: def fact1(n): if n < 2: return 1 else: return n * fact1(n-1) def fact2(n): if n < 2: return 1 return n * fact2(n-1) They should be equivalent. But there's a performance difference: >>> T(lambda : fact1(1)).repeat(number=10000000) [2.5754408836364746, 2.5710129737854004, 2.5678811073303223] >>> T(lambda : fact2(1)).repeat(number=10000000) [2.8432059288024902, 2.834425926208496, 2.8364310264587402] The version without the else is 10% slower. This is pretty significant. Why?
Tags (comma-separated)
Save Edits
Cancel