-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprofile_util.py
72 lines (48 loc) · 1.44 KB
/
profile_util.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (C) 2023 IBM Corp.
# SPDX-License-Identifier: Apache-2.0
import functools
from ulkb import util
from .profiler import Profiler
def sum_v1(it):
total = 0
for n in it:
total += n
return total
def sum_v2(it, f=lambda x, y: x + y):
total = 0
for n in it:
total = f(total, n)
return total
def reduce(f, x, xs):
return functools.reduce(f, xs, x)
def foldl(f, x, xs):
for y in iter(xs):
x = f(x, y)
return x
def foldr_v1(f, x, xs):
return reduce(util.flip(f), x, reversed(xs))
def foldr_v2(f, x, xs):
for y in reversed(xs):
x = f(y, x)
return x
def foldr1_v1(f, xs):
return functools.reduce(util.flip(f), reversed(xs))
def foldr1_v2(f, xs):
it = reversed(xs)
x = next(it)
for y in it:
x = f(y, x)
return x
total = sum(range(0, 100))
input = 'range(0, 100)'
pf = Profiler(globals())
pf.timeit(
f'assert sum_v1({input}) == {total}',
f'assert sum_v2({input}) == {total}',
f'assert reduce(lambda x, y: x + y, 0, {input}) == {total}',
f'assert foldl(lambda x, y: x + y, 0, {input}) == {total}',
f'assert foldr_v1(lambda x, y: x + y, 0, {input}) == {total}',
f'assert foldr_v2(lambda x, y: x + y, 0, {input}) == {total}',
f'assert foldr1_v1(lambda x, y: x + y, {input}) == {total}',
f'assert foldr1_v2(lambda x, y: x + y, {input}) == {total}')
# pf.profile(lambda: util.foldr(lambda x, y: x + y, 0, range(0, 1000)))