Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Property performance vs direct attribute access.
- '''
- class A(object):
- def __init__(self, a):
- self._a = a
- def __eq__(self, other):
- return self.a == other.a
- @property
- def a(self):
- return self._a
- def test_prop(n):
- a = A(1)
- for i in range(n):
- a.a + 1
- def test_attr(n):
- a = A(1)
- for i in range(n):
- a._a + 1
- import cProfile
- n = 10**5
- cProfile.run('test_prop(n)')
- cProfile.run('test_attr(n)')
- '''
- 100005 function calls in 0.361 seconds
- Ordered by: standard name
- ncalls tottime percall cumtime percall filename:lineno(function)
- 1 0.000 0.000 0.361 0.361 <string>:1(<module>)
- 100000 0.085 0.000 0.085 0.000 tt.py:14(a)
- 1 0.268 0.268 0.361 0.361 tt.py:19(test_prop)
- 1 0.000 0.000 0.000 0.000 tt.py:8(__init__)
- 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
- 1 0.008 0.008 0.008 0.008 {range}
- 5 function calls in 0.063 seconds
- Ordered by: standard name
- ncalls tottime percall cumtime percall filename:lineno(function)
- 1 0.000 0.000 0.063 0.063 <string>:1(<module>)
- 1 0.058 0.058 0.063 0.063 tt.py:25(test_attr)
- 1 0.000 0.000 0.000 0.000 tt.py:8(__init__)
- 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
- 1 0.005 0.005 0.005 0.005 {range}
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement