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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:45:23+00:00 2026-06-11T12:45:23+00:00

While trying to tackle a more complex problem, I came to compare access speed

  • 0

While trying to tackle a more complex problem, I came to compare access speed to local variable vs member variables.

Here a test program:

#!/usr/bin/env python

MAX=40000000

class StressTestMember(object):
    def __init__(self):
        self.m = 0

    def do_work(self):
        self.m += 1
        self.m *= 2

class StressTestLocal(object):
    def __init__(self):
        pass

    def do_work(self):
        m = 0
        m += 1
        m *= 2

# LOCAL access test
for i in range(MAX):
    StressTestLocal().do_work()

# MEMBER access test
for i in range(MAX):
    StressTestMember().do_work()

I know it might look like a bad idea to instantiate StressTestMember and StressTestLocal on each iterations but it makes sense in the modeled program where these are basically Active Records.

After a simple benchmark,

  • LOCAL access test: 0m22.836
  • MEMBER access test: 0m32.648s

The local version is ~33% faster while still part of a class. 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-06-11T12:45:24+00:00Added an answer on June 11, 2026 at 12:45 pm

    self.m += 1 means you have to look up a local variable called self and then find the attribute called m

    Of course if you just have to look up a local variable, it will be faster without the extra step.

    It can be useful to look at what is happening under the hood:

    >>> import dis
    >>> dis.dis(StressTestLocal.do_work)
     18           0 LOAD_CONST               1 (0)
                  3 STORE_FAST               1 (m)
    
     19           6 LOAD_FAST                1 (m)
                  9 LOAD_CONST               2 (1)
                 12 INPLACE_ADD         
                 13 STORE_FAST               1 (m)
    
     20          16 LOAD_FAST                1 (m)
                 19 LOAD_CONST               3 (2)
                 22 INPLACE_MULTIPLY    
                 23 STORE_FAST               1 (m)
                 26 LOAD_CONST               0 (None)
                 29 RETURN_VALUE        
    >>> dis.dis(StressTestMember.do_work)
     10           0 LOAD_FAST                0 (self)
                  3 DUP_TOP             
                  4 LOAD_ATTR                0 (m)
                  7 LOAD_CONST               1 (1)
                 10 INPLACE_ADD         
                 11 ROT_TWO             
                 12 STORE_ATTR               0 (m)
    
     11          15 LOAD_FAST                0 (self)
                 18 DUP_TOP             
                 19 LOAD_ATTR                0 (m)
                 22 LOAD_CONST               2 (2)
                 25 INPLACE_MULTIPLY    
                 26 ROT_TWO             
                 27 STORE_ATTR               0 (m)
                 30 LOAD_CONST               0 (None)
                 33 RETURN_VALUE        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem I've been trying to tackle for a while. I have
While trying to solve a two-way data binding problem in here , I found
I have a problem. I've been trying to tackle it for a while now
While trying to troubleshoot a problem I'm having with a project, I noticed that
While trying to start local Google App Engine (v1.3.8) Server on my Fedora 14
I'm trying to tackle a problem that seemingly many Android developers have, which is
While trying to setup a synchronization for a bitbucket repo on my local server,
I have a problem that I have been trying to tackle for the last
While trying to optimize some code I came across another question.. Using usergroups and
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, 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.