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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:05:30+00:00 2026-06-01T03:05:30+00:00

I thought that I improve performance when I replace this code: def f(a, b):

  • 0

I thought that I improve performance when I replace this code:

def f(a, b):
  return math.sqrt(a) * b
result = []
a = 100
for b in range(1000000):
  result.append(f(a, b))

with:

def g(a):
  def f(b):
    return math.sqrt(a) * b
  return f
result = []
a = 100
func = g(a)
for b in range(1000000):
  result.append(func(b))

I assumed that since a is fixed when the closure is performed, the interpreter would precompute everything that involves a, and so math.sqrt(a) would be repeated just once instead of 1000000 times.

Is my understanding always correct, or always incorrect, or correct/incorrect depending on the implementation?

I noticed that the code object for func is built (at least in CPython) before runtime, and is immutable. The code object then seems to use global environment to achieve the closure. This seems to suggest that the optimization I hoped for does not happen.

  • 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-01T03:05:31+00:00Added an answer on June 1, 2026 at 3:05 am

    I assumed that since a is fixed when the closure is performed, the interpreter would precompute everything that involves a, and so
    math.sqrt(a) would be repeated just once instead of 1000000 times.

    That assumption is wrong, I don’t know where it came from. A closure just captures variable bindings, in your case it captures the value of a, but that doesn’t mean that any more magic is going on: The expression math.sqrt(a) is still evaluated every time f is called.

    After all, it has to be computed every time because the interpreter doesn’t know that sqrt is “pure” (the return value is only dependent on the argument and no side-effects are performed). Optimizations like the ones you expect are practical in functional languages (referential transparency and static typing help a lot here), but would be very hard to implement in Python, which is an imperative and dynamically typed language.

    That said, if you want to precompute the value of math.sqrt(a), you need to do that explicitly:

    def g(a):
      s = math.sqrt(a)
      def f(b):
        return s * b
      return f
    

    Or using lambda:

    def g(a): 
      s = math.sqrt(a)
      return lambda b: s * b
    

    Now that g really returns a function with 1 parameter, you have to call the result with only one argument.

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

Sidebar

Related Questions

I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
I always thought that parentheses improved readability, but in my textbook there is a
I thought that a delegate instance was interchangeable with a function instance. Take the
I thought that by default my Regex would exhibit the greedy behavior that I
I thought that calling BeginInvoke more than once on the same delegate instance would
I thought that layout is just a widget that keeps more widgets inside. But
I thought that in principle Haskell's type system would forbid calls to impure functions
I thought that the purpose of Ruby's BigDecimal class is that it is infinitely
I have always thought that in order to connect to SQL server using windows
I am trying to improve the performance of a SSIS Package. One thing I

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.