- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_functions.py
More file actions
Latest commit
197 lines (156 loc) · 4.8 KB
/
Copy pathexample_functions.py
File metadata and controls
197 lines (156 loc) · 4.8 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Example to show basic inlining with the function decorators
# of InlineGenerator.
#
# Use `c_function` to declare a C function. The function is
# callable from Python.
# Use `callable_function` to import a Python function into the
# C namespace.
#
# NOTE: For trivial tasks the overhead of the ctype conversions
# will let the C code run slower than a CPython equivalent.
from __future__ importprint_function
fromtinyccimportTinyCC, InlineGenerator
fromctypesimportc_int
try:
range=xrange
exceptNameError:
pass
# create a generator object first
gen=InlineGenerator()
# do the inline definitions with the provided decorators
@gen.callable_function(c_int, c_int)
defr_fib_py(n):
ifn<=2:
return1
returnr_fib_py(n-1) +r_fib_py(n-2)
@gen.c_function(c_int, c_int)
defr_fib_c(n):
"""
if (n <= 2)
return 1;
return r_fib_c(n-1) + r_fib_c(n-2);
"""
@gen.callable_function(c_int, c_int)
defl_fib_py(a):
result=0
ifa<=2:
return1
last=next_to_last=1
foriinrange(2, a):
result=last+next_to_last
next_to_last=last
last=result
returnresult
@gen.c_function(c_int, c_int)
defl_fib_c(a):
"""
int last, next_to_last, result = 0;
if(a <= 2)
return 1;
last = next_to_last = 1;
for(int i=2; i<a; ++i) {
result = last + next_to_last;
next_to_last = last;
last = result;
}
return result;
"""
@gen.c_function(c_int, c_int)
defc_runner_r(n):
"""
int i, sum = 0;
for (i=0; i<n; ++i) {
sum += r_fib_py(20);
}
return sum;
"""
@gen.c_function(c_int, c_int)
defc_runner_l(n):
"""
int i, sum = 0;
for (i=0; i<n; ++i) {
sum += l_fib_py(20);
}
return sum;
"""
defpy_runner_r(n):
sum=0
for_inrange(n):
sum+=r_fib_py(20)
returnsum
defpy_runner_l(n):
sum=0
for_inrange(n):
sum+=l_fib_py(20)
returnsum
@gen.c_function(c_int, c_int)
defc_runner_rc(n):
"""
int i, sum = 0;
for (i=0; i<n; ++i) {
sum += r_fib_c(20);
}
return sum;
"""
@gen.c_function(c_int, c_int)
defc_runner_lc(n):
"""
int i, sum = 0;
for (i=0; i<n; ++i) {
sum += l_fib_c(20);
}
return sum;
"""
defpy_runner_rc(n):
sum=0
for_inrange(n):
sum+=r_fib_c(20)
returnsum
defpy_runner_lc(n):
sum=0
for_inrange(n):
sum+=l_fib_c(20)
returnsum
if__name__=='__main__':
state=TinyCC().create_state()
# compile the generated code
state.compile(gen.code)
state.relocate()
# bind state to the generator object
# needed to resolve symbols from and to C
gen.bind_state(state)
# ready to use
assert(r_fib_py(20) ==r_fib_c(20))
assert(l_fib_py(20) ==l_fib_c(20))
assert (r_fib_c(20) ==l_fib_c(20))
fromtimeitimporttimeit
py=timeit('r_fib_py(20)', setup='from __main__ import r_fib_py', number=100)
c=timeit('r_fib_c(20)', setup='from __main__ import r_fib_c', number=100)
print('recursive Python/C:', py, c, float(py) /c)
py=timeit('l_fib_py(20)', setup='from __main__ import l_fib_py', number=100)
c=timeit('l_fib_c(20)', setup='from __main__ import l_fib_c', number=100)
print('looped Python/C:', py, c, float(py) /c)
# some call tests Python <--> C
print('workload - sum(100x fib(20)):')
assert(c_runner_l(100) ==py_runner_l(100))
assert(c_runner_l(100) ==py_runner_r(100))
assert(c_runner_l(100) ==c_runner_rc(100))
assert(c_runner_l(100) ==c_runner_lc(100))
assert(c_runner_l(100) ==py_runner_rc(100))
assert(c_runner_l(100) ==py_runner_lc(100))
assert(c_runner_r(100) ==c_runner_l(100))
r=timeit('c_runner_r(100)', setup='from __main__ import c_runner_r', number=1)
l=timeit('c_runner_l(100)', setup='from __main__ import c_runner_l', number=1)
print('fib_py from C (rec/loop):', r, l, float(r) /l)
assert(py_runner_r(100) ==py_runner_l(100))
r=timeit('py_runner_r(100)', setup='from __main__ import py_runner_r', number=1)
l=timeit('py_runner_l(100)', setup='from __main__ import py_runner_l', number=1)
print('fib_py from Py (rec/loop):', r, l, float(r) /l)
assert(c_runner_rc(100) ==c_runner_lc(100))
r=timeit('c_runner_rc(100)', setup='from __main__ import c_runner_rc', number=1)
l=timeit('c_runner_lc(100)', setup='from __main__ import c_runner_lc', number=1)
print('fib_c from C (rec/loop):', r, l, float(r) /l)
assert(py_runner_rc(100) ==py_runner_lc(100))
r=timeit('py_runner_rc(100)', setup='from __main__ import py_runner_rc', number=1)
l=timeit('py_runner_lc(100)', setup='from __main__ import py_runner_lc', number=1)
print('fib_c from Py (rec/loop):', r, l, float(r) /l)