Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:37:42+00:00 2026-05-27T00:37:42+00:00

Consider the following functions: def fact1(n): if n < 2: return 1 else: return

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:37:42+00:00Added an answer on May 27, 2026 at 12:37 am

    For me, they are virtually the same speed: (Python 2.6.6 on Debian)

    In [4]: %timeit fact1(1)
    10000000 loops, best of 3: 151 ns per loop
    
    In [5]: %timeit fact2(1)
    10000000 loops, best of 3: 154 ns per loop
    

    The byte code is also very similar:

    In [6]: dis.dis(fact1)
      2           0 LOAD_FAST                0 (n)
                  3 LOAD_CONST               1 (2)
                  6 COMPARE_OP               0 (<)
                  9 JUMP_IF_FALSE            5 (to 17)
                 12 POP_TOP             
    
      3          13 LOAD_CONST               2 (1)
                 16 RETURN_VALUE        
            >>   17 POP_TOP             
    
      5          18 LOAD_FAST                0 (n)
                 21 LOAD_GLOBAL              0 (fact)
                 24 LOAD_FAST                0 (n)
                 27 LOAD_CONST               2 (1)
                 30 BINARY_SUBTRACT     
                 31 CALL_FUNCTION            1
                 34 BINARY_MULTIPLY     
                 35 RETURN_VALUE        
                 36 LOAD_CONST               0 (None)
                 39 RETURN_VALUE        
    
    In [7]: dis.dis(fact2)
      2           0 LOAD_FAST                0 (n)
                  3 LOAD_CONST               1 (2)
                  6 COMPARE_OP               0 (<)
                  9 JUMP_IF_FALSE            5 (to 17)
                 12 POP_TOP             
    
      3          13 LOAD_CONST               2 (1)
                 16 RETURN_VALUE        
            >>   17 POP_TOP             
    
      4          18 LOAD_FAST                0 (n)
                 21 LOAD_GLOBAL              0 (fact)
                 24 LOAD_FAST                0 (n)
                 27 LOAD_CONST               2 (1)
                 30 BINARY_SUBTRACT     
                 31 CALL_FUNCTION            1
                 34 BINARY_MULTIPLY     
                 35 RETURN_VALUE        
    

    The only difference is that the version with the else includes code to return None in case control reaches the end of the function body.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: def CalcSomething(a): if CalcSomething._cache.has_key(a): return CalcSomething._cache[a] CalcSomething._cache[a] = ReallyCalc(a) return
consider the following code if insert() return the list itself. def sieve(l): if not
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
Consider the following script: def a = new HashSet() def str1 = str1 def
Consider the following situation file: ./include/functions/table-config.php containing: . . $tablePages = 'orweb_pages'; . .
Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
The question is to consider the following function definition: def read_value_type(prompt='Enter a Boolean> ',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.