annotate tests/_perf_lookups.py @ 654:0d6673d06c2c

Add support for using "tomllib" (in Python's stdlib since 3.11) and "tomli" TOML packages. They are preferred if they are found to be installed. But note that the declared dependency for the "toml" extra nevertheless is the "toml" package. Because it is available for all supported Python versions. So use Python 3.11+ or install "tomli" manually if you want to use the alternate packages.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 19 May 2022 22:10:59 +0200
parents 60683361ebed
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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) )