Home >Backend Development >Python Tutorial >How much better are python local variables over globals, attributes, or slots?

How much better are python local variables over globals, attributes, or slots?

Barbara Streisand
Barbara StreisandOriginal
2024-12-28 02:07:11618browse

How much better are python local variables over globals, attributes, or slots?

Alternatively, how much worse are the others compared to local variables?

What's the big deal?

Some articles advise python programmers to use local variables if they will be used a lot, even if only referenced but not changed.

For example, here are two quotes from "Making Python Programs Blazing Fast" :

"There's actually difference in speed of lookup even between - let's say - local variable in function (fastest), class-level attribute (e.g. self.name - slower) and global for example imported function like time.time (slowest)."

"Don't Access Attributes

Another thing that might slow down your programs is dot operator (.) which is used when accessing object attributes. This operator triggers dictionary lookup using getattribute, which creates extra overhead in your code. "

Also, these are not the only possibilities. What about differences among:

  • Class attributes,
  • Instance attributes,
  • Slot Instance attributes,
  • Method attributes, and
  • Function (non-method) attributes?

Testing

I tested by using all these in loops with 1,000,000 iterations, but minimal work per iteration inside them. The functions were all methods of a class, except for the function used to test function attributes for functions outside a class.

A typical function was of the form:

      def testINSA(self):
          s=0
          for i in range(10000000):
            s+=self.INSA # what is after the = differs in each function.
          return s    

All the code is at the bottom of this post.

Results

|                                        |      |        | Relative |
| Where                                  | Time | Rate   | Performance |
| LV Local Variable                      | 1.92 | 0.5208 | 100% |
| GV Global Variable                     | 1.99 | 0.5020 | 96% |
| ISA Instance Slotted Attribute         | 2.09 | 0.4789 | 92% |
| CA Class Attribute                     | 3.12 | 0.3204 | 62% |
| INSA Instance Non-Slotted Attribute    | 3.28 | 0.3051 | 59% |
| FA Function Attribute              | 3.49 | 0.2865 | 55% |
| MA Method Attribute                | 6.29 | 0.1589 | 31% |

Explanation:
When comparing performance, always compare the (Achievements / Resource), such as Miles per Gallon, or Calculations per Second, rather than Liters per Km, or Seconds per Calculation.

  • Time is as reported by timeit, limited to 3 significant digits.
  • Rate is the reciprocal of time. How many of these per second.
  • Relative performance is the rate as compared to the best. By using relative performance, bigger is better, and twice as big is twice as good.

Findings

Yes, the local variables are the fastest.

The performance of the different types of variables clustered into three groupings.

  1. Locals, globals, and class instance slotted variables: Global variables are 96% as fast as locals, while class instance slotted attributes are 92% as fast.
  2. Class Attribute, non-slotted class instance attribute, and function attribute: These perform, respectively, at 62%, 59%, and 55% of a local variable.
  3. Method attribute: Using a method attribute is in a class of its own, at 31% of the performance of a local variable.

Conclusions

The surprise is that compared to common wisdom, globals are second best, better even than slotted instance attributes in a class. Another surprise is that method attributes are the worst.

Code

      def testINSA(self):
          s=0
          for i in range(10000000):
            s+=self.INSA # what is after the = differs in each function.
          return s    

The above is the detailed content of How much better are python local variables over globals, attributes, or slots?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn