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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:10:37+00:00 2026-05-27T08:10:37+00:00

I use numpy.random.normal function in a tough loop in a class. class MyClass(MyBaseClass): def

  • 0

I use numpy.random.normal function in a tough loop in a class.

class MyClass(MyBaseClass):   
    def run(self):
        while True:
            ...
            self.L.append(numpy.random.normal(0,1))

I know that it’s pretty slow in Python to use multiple lookups. In numpy.random.normal there are 3 lookups: first numpy is looked up, then random, and then normal.

So I decided to address this problem by assigning numpy.random.normal to a local variable _normal.

Here we go:

class MyClass(MyBaseClass):
    _normal = numpy.random.normal
    def run(self):
        while True:
            ...
            self.L.append(MyClass._normal(0,1))

What I’m really concerned about is descriptors. When a variable in a class in accessed, all the bases classes are looked up for the data descriptor with the same name. It’s described here:

Check objectname.__class__.__dict__for attrname. If it exists and is a data-descriptor, return the descriptor result. Search all bases of objectname.__class__ for the same case.

So, I guess, if I put _normal in the local space as I did above, it will case looking up all bases classes for the data descriptor. And I wary of it becoming a source of a slowdown.

Are my concerns justified?

Should I worry about the time it takes to look up for the descriptors in base classes?

And is there a better way to speed up access to a function located deep into a module when it’s used in a class?


There was a discussion in the comments to the answers.

I decided to give some additional details of implementation that appeared to be important (for my particular case).

Actually, the code is closer to this (it’s very very simplified):

class MyClass(MyBaseClass):

    def __iter__(self):
        return self

    def next(self):
        self.L.append(numpy.random.normal(0,1))   

    def run(self):
        while True:
            self.next()
  • 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-27T08:10:37+00:00Added an answer on May 27, 2026 at 8:10 am

    If you must do something like this (is function lookup actually the dominant cost? Random number generation is not cheap) you should realize that one global + one attr lookup (MyClass._normal) is not that much cheaper than one global + three attr lookups (numpy.random.normal). What you really want is to get zero global or attr lookups inside the loop, which you can only do by defining _normal inside the function. If you’re really desperate to shave cycles you should also prebind the list append call:

    class MyClass(MyBaseClass):
        def run(self):
            _normal = numpy.random.normal
            _Lappend = self.L.append
            while True:
                ...
                _Lappend(_normal(0,1))
    

    Contrast disassembly output (just for the append statement):

      LOAD_FAST                0 (self)
      LOAD_ATTR                1 (L)
      LOAD_ATTR                2 (append)
      LOAD_GLOBAL              3 (numpy)
      LOAD_ATTR                4 (random)
      LOAD_ATTR                5 (normal)
      LOAD_CONST               1 (0)
      LOAD_CONST               2 (1)
      CALL_FUNCTION            2
      CALL_FUNCTION            1
      POP_TOP             
    

    vs

      LOAD_FAST                2 (_Lappend)
      LOAD_FAST                1 (_normal)
      LOAD_CONST               1 (0)
      LOAD_CONST               2 (1)
      CALL_FUNCTION            2
      CALL_FUNCTION            1
    

    What would be even better is to vectorize — generate many random normal deviates from and append them to the list in one go — you can do that with the size argument to numpy.random.normal.

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

Sidebar

Related Questions

So far I've come up with following: screen_array = pygame.surfarray.pixels2d(self.screen) noise_small = numpy.random.random((self.width/4,self.height/4)) *
How can I use the same random number generator in my Python with numpy
One can use numpy 's extract function to match an element in an array.
use Rack::Static, :urls => ['/stylesheets', '/images'], :root => 'public' run proc { |env| [200,
I use numpy for numerical linear algebra. I suspect that I can get much
How would I use numpy to calculate the intersection between two line segments? In
I'm thinking I need to use numpy or some other library to fill these
numpy has a function to set the specified elements to be some value a=numpy.zeros(10)
One may select elements in numpy arrays as follows a = np.random.rand(100) sel =
When you use the POISSON function in Excel (or in OpenOffice Calc), it takes

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.