Mercurial > hgrepos > Python > libs > ConfigMix
annotate tests/_perf_lookups.py @ 494:60683361ebed
Test-script for checking the costs of different variable lookup methods
Variant from https://stackoverflow.com/questions/2714573/instance-variables-vs-class-variables-in-python
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Fri, 17 Dec 2021 19:24:54 +0100 |
| parents | |
| children |
| rev | line source |
|---|---|
|
494
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
1 from __future__ import print_function |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
2 import timeit |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
3 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
4 setup=''' |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
5 XGLOBAL= 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
6 class A: |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
7 xclass = 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
8 def __init__(self): |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
9 self.xinstance = 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
10 def f1(self): |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
11 xlocal = 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
12 x = self.xinstance |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
13 def f2(self): |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
14 xlocal = 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
15 x = A.xclass |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
16 def f3(self): |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
17 xlocal = 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
18 x = XGLOBAL |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
19 def f4(self): |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
20 xlocal = 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
21 x = xlocal |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
22 def f5(self): |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
23 xlocal = 5 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
24 x = self.xclass |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
25 a = A() |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
26 ''' |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
27 num = 300000000 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
28 num = 90000000 |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
29 print('access via instance variable: %.3f' % timeit.timeit('a.f1()', setup=setup, number=num) ) |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
30 print('access via class variable (via classname): %.3f' % timeit.timeit('a.f2()', setup=setup, number=num) ) |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
31 print('access via class variable (via self): %.3f' % timeit.timeit('a.f5()', setup=setup, number=num) ) |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
32 print('access via module variable: %.3f' % timeit.timeit('a.f3()', setup=setup, number=num) ) |
|
60683361ebed
Test-script for checking the costs of different variable lookup methods
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
33 print('access via local variable: %.3f' % timeit.timeit('a.f4()', setup=setup, number=num) ) |
