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 9091445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:36:46+00:00 2026-06-16T22:36:46+00:00

I prefer using nested functions instead of methods or global functions in Python whenever

  • 0

I prefer using nested functions instead of methods or global functions in Python whenever possible. So I decided to test their performance because it seams that when you define a function in another function there will be an overhead for the definition of the inner function in each call of the outer function.

At best I was hoping for the global function to be just slightly faster, but surprisingly the nested function was faster. Does anyone have any idea why?

This is my code:

from time import clock

def a(n):
    return n + 1

def b1(loopcount):
    return sum([a(n) for n in range(loopcount)])

def b2(loopcount):
    def a(n):
        return n + 1
    return sum([a(n) for n in range(loopcount)])

powers = [5, 6, 7]
b1times = []
b2times = []
print "   ", "".join(["{:^10d}".format(n) for n in powers])    
for i in range(5):
    for power in powers:
        t = clock()
        b1(10**power)
        b1times.append(clock() - t)
    for power in powers:
        t = clock()
        b2(10**power)
        b2times.append(clock() - t)
    print "b1:", "".join(["{:^10.5f}".format(n) for n in b1times])
    print "b2:", "".join(["{:^10.5f}".format(n) for n in b2times])
    print ""
    b1times = []
    b2times = [] 

And this is the result on my computer:

        5         6         7
b1:  0.08200   0.82773   8.47946
b2:  0.06914   0.79637   8.18571

b1:  0.07332   0.82139   8.68262
b2:  0.06547   0.82088   8.19606

b1:  0.07963   0.82625   9.65037
b2:  0.06617   0.82027   8.21412

b1:  0.07630   0.82112   8.49082
b2:  0.06541   0.80686   8.20532

b1:  0.12328   0.87034   8.42964
b2:  0.07059   0.79717   8.24620

UPDATE: using @Janne Karila’s comment

Now that I’m calling b1 and b2 more, b1 becomes faster. So as @Kos and @Pavel Anossov said in their answers a few factors affect the speed here and you can’t make a general statement.
Thanks everyone!

from time import *

def a1(n):
    return n + 1

def b1(n):
    return a1(n)

def b2(n):
    def a2():
        return n + 1
    return a2()

powers = [4, 5, 6]
b1times = []
b2times = []
print "   ", "".join(["{:^10d}".format(n) for n in powers])    
for i in range(5):
    for power in powers:
        t = clock()
        sum([b1(n) for n in range(10**power)])
        b1times.append(clock() - t)
    for power in powers:
        t = clock()
        sum([b2(n) for n in range(10**power)])
        b2times.append(clock() - t)
    print "b1:", "".join(["{:^10.5f}".format(n) for n in b1times])
    print "b2:", "".join(["{:^10.5f}".format(n) for n in b2times])
    print ""
    b1times = []
    b2times = [] 
  • 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-06-16T22:36:47+00:00Added an answer on June 16, 2026 at 10:36 pm

    There are several parts that have an effect here:

    1) The time to define the function (create the function object),
    2) The time to look up the function object by name,
    3) The time to actually call the function.

    Your global function example is faster with 1) (no need to redefine a in each call to b1). However, it is slower with in 2) because global variable lookup is slower than local lookup.

    Why can’t we have both then?

    I’ve extended your benchmark with a solution that uses the global function, but avoids the global lookup using a local variable. It seems to be the fastest of the three on my machine:

            5         6         7
    b1:  0.04147   0.44421   4.46508
    b2:  0.03399   0.43321   4.41121
    b3:  0.03258   0.41821   4.25542
    
    b1:  0.03240   0.42998   4.39774
    b2:  0.03320   0.43465   4.42229
    b3:  0.03155   0.42109   4.23669
    
    b1:  0.03273   0.43321   4.37266
    b2:  0.03326   0.43551   4.42208
    b3:  0.03137   0.42356   4.25341
    
    b1:  0.03253   0.43104   4.40466
    b2:  0.03401   0.43719   4.42996
    b3:  0.03155   0.41681   4.24132
    
    b1:  0.03244   0.42965   4.37192
    b2:  0.03310   0.43629   4.42727
    b3:  0.03117   0.41701   4.23932
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I prefer using Notepad++ for developing, How do I execute the files in Python
Windows 7 As I'm learning Python (3.2) I prefer using IDLE than CMD. In
I'd prefer using Table and td instead of JqGrid but i like JqGrid styles.
Possible Duplicate: Why would I prefer using vector to deque I am curious why
When debugging (Juno, SR1 on mac) I prefer using the keyboard keys instead of
What do you think about using private static methods ? Personally, I prefer using
I much prefer using this 'embedded' style inserts in a pl/sql block (opposed to
what do you prefer for testing JavaScript apps using Cucumber? I never did it
I have just started using C2DM. I prefer to send REGISTRATION intent only once
Usually I prefer to write my own solutions for trivial problems because generally plugins

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.