forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecial-len.py
More file actions
48 lines (32 loc) · 810 Bytes
/
special-len.py
File metadata and controls
48 lines (32 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# special method __len__ dispatch
import time
class Num(object):
def __init__(self, n):
self.n = n
def __len__(self):
return self.n
def __repr__(self):
return repr(self.n)
def do_compute(num):
for i in range(num):
sum_ = 0
one = Num(42)
j = 0
while j < num:
sum_ = sum_ + len(one)
j += 1
return sum_
def measure(num):
for run in range(3):
do_compute(20000) # 10000
print("Start timing...")
start = time.time()
for run in range(num):
sum_ = do_compute(20000) # 10000
print("sum", sum_)
duration = "%.3f\n" % (time.time() - start)
print("special-len: " + duration)
print('warming up ...')
for run in range(2000):
do_compute(5) # 1000
measure(5)