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

  • SEARCH
  • Home
  • 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 8590057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:12:22+00:00 2026-06-11T23:12:22+00:00

I am still new to Python, and I have been trying to improve the

  • 0

I am still new to Python, and I have been trying to improve the performance of my Python script, so I tested it with and without global variables. I timed it, and to my surprise, it ran faster with global variables declared rather than passing local vars to functions. What’s going on? I thought execution speed was faster with local variables? (I know globals are not safe, I am still curious.)

  • 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-11T23:12:23+00:00Added an answer on June 11, 2026 at 11:12 pm

    Locals should be faster

    According to this page on locals and globals:

    When a line of code asks for the value of a variable x, Python will search for that variable in all the available namespaces, in order:

    • local namespace – specific to the current function or class method. If the function defines a local variable x, or has an argument x, Python will use this and stop searching.
    • global namespace – specific to the current module. If the module has defined a variable, function, or class called x, Python will use that and stop searching.
    • built-in namespace – global to all modules. As a last resort, Python will assume that x is the name of built-in function or variable.

    Based on that, I’d assume that local variables are generally faster. My guess is what you’re seeing is something particular about your script.

    Locals are faster

    Here’s a trivial example using a local variable, which takes about 0.5 seconds on my machine (0.3 in Python 3):

    def func():
        for i in range(10000000):
            x = 5
    
    func()
    

    And the global version, which takes about 0.7 (0.5 in Python 3):

    def func():
        global x
        for i in range(1000000):
            x = 5
    
    func()
    

    global does something weird to variables that are already global

    Interestingly, this version runs in 0.8 seconds:

    global x
    x = 5
    for i in range(10000000):
        x = 5
    

    While this runs in 0.9:

    x = 5
    for i in range(10000000):
        x = 5
    

    You’ll notice that in both cases, x is a global variable (since there’s no functions), and they’re both slower than using locals. I have no clue why declaring global x helped in this case.

    This weirdness doesn’t occur in Python 3 (both versions take about 0.6 seconds).

    Better optimization methods

    If you want to optimize your program, the best thing you can do is profile it. This will tell you what’s taking the most time, so you can focus on that. Your process should be something like:

    1. Run your program with profiling on.
    2. Look at the profile in KCacheGrind or a similar program to determine Which functions are taking the most time.
    3. In those functions:
      • Look for places where you can cache results of functions (so you don’t have to do as much work).
      • Look for algorithmic improvements like replacing recursive functions with closed-form functions, or replacing list searches with dictionaries.
      • Re-profile to make sure the function is still a problem.
      • Consider using multiprocessing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am still new to Python and have been reviewing the following code not
I have been doing a bit of programming in Python (still a n00b at
New to python here. I've been pulling my hair for hours and still can't
I am new to python and have been working through the examples in Swaroop
Still new to python and django, though learning ;-) I have a view that
I'm still fairly new to Python and I'm trying to get used to its
I have been trying for months to get numpy installed for Python 3 but
I have been receiving errors when trying to save and run this Python 3.1
I'm new to Pythong and I have been trying to get a button within
still new to XML parsing with the iphone so i have a few questions.

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.