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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:57:49+00:00 2026-05-26T21:57:49+00:00

This is pretty bad micro-optimizing, but I’m just curious. It usually doesn’t make a

  • 0

This is pretty bad micro-optimizing, but I’m just curious. It usually doesn’t make a difference in the “real” world.

So I’m compiling a function (that does nothing) using compile() then calling exec on that code and getting a reference to the function I compiled. Then I’m executing it a couple million times and timing it. Then repeating it with a local function. Why is the dynamically compiled function around 15% slower (on python 2.7.2) for just the call?

import datetime
def getCompiledFunc():
  cc = compile("def aa():pass", '<string>', 'exec')
  dd = {}
  exec cc in dd
  return dd.get('aa')

compiledFunc = getCompiledFunc()  
def localFunc():pass


def testCall(f):
  st = datetime.datetime.now()
  for x in xrange(10000000): f()
  et = datetime.datetime.now()
  return (et-st).total_seconds()

for x in xrange(10):
  lt = testCall(localFunc)
  ct = testCall(compiledFunc)
  print "%s %s %s%% slower" % (lt, ct, int(100.0*(ct-lt)/lt))

The output I’m getting is something like:

1.139 1.319 15% slower
  • 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-26T21:57:50+00:00Added an answer on May 26, 2026 at 9:57 pm

    The dis.dis() function shows that the code object for each version is identical:

    aa
      1           0 LOAD_CONST               0 (None)
                  3 RETURN_VALUE        
    localFunc
     10           0 LOAD_CONST               0 (None)
                  3 RETURN_VALUE 
    

    So the difference is in the function object. I compared each of the fields (func_doc, func_closure, etc) and the one that is different is func_globals. In other words, localFunc.func_globals != compiledFunc.func_globals.

    There is a cost for supplying your own dictionary instead of the built-in globals (the former has to be looked up when a stack frame is created on each call and the latter can be referenced directly by the C code which already knows about the default builtin globals dictionary).

    This is easy verified by changing the exec line in your code to:

    exec cc in globals(), dd
    

    With that change, the timing difference goes away.

    Mystery solved!

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

Sidebar

Related Questions

This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
This is pretty simple, I come from a swing/awt background. I'm just wondering what
This is pretty trivial, but I noticed on SO that instead of an offset
Yes, I realize this is horrible UI and bad accessibility wise, but I am
Hi getting variable peformance & effect between firefox (pretty bad) chrome (ok) Sometimes this
My last experience with WCF 3.0 was pretty bad, because of this I reverted
another Objective-C one for you, probably pretty obvious but I have been at this
The title is pretty bad but heres the general idea of what I want
Been trying hard to find a solution to this, but the results are pretty
I am having a pretty bad night with this one. I have been running

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.