Skip to content

Commit f01fa45

Browse files
committed
tests/bench/var: Add tests for class/instance var access.
Also compared with method abstraction for accessing instance vars - it's more than 3 times slower than accessing var directly.
1 parent aaff82a commit f01fa45

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

tests/bench/var-5-class-attr.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import bench
2+
3+
class Foo:
4+
num = 20000000
5+
6+
def test(num):
7+
i = 0
8+
while i < Foo.num:
9+
i += 1
10+
11+
bench.run(test)

tests/bench/var-6-instance-attr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import bench
2+
3+
class Foo:
4+
5+
def __init__(self):
6+
self.num = 20000000
7+
8+
def test(num):
9+
o = Foo()
10+
i = 0
11+
while i < o.num:
12+
i += 1
13+
14+
bench.run(test)

tests/bench/var-7-instance-meth.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import bench
2+
3+
class Foo:
4+
5+
def __init__(self):
6+
self._num = 20000000
7+
8+
def num(self):
9+
return self._num
10+
11+
def test(num):
12+
o = Foo()
13+
i = 0
14+
while i < o.num():
15+
i += 1
16+
17+
bench.run(test)

0 commit comments

Comments
 (0)