2
# cython: profile = True
3
# cython: binding = False
6
>>> import os, tempfile, cProfile as profile, pstats
7
>>> statsfile = tempfile.mkstemp()[1]
8
>>> profile.runctx("test_profile(100)", locals(), globals(), statsfile)
9
>>> s = pstats.Stats(statsfile)
10
>>> short_stats = dict([(k[2], v[1]) for k,v in s.stats.items()])
11
>>> short_stats['f_def']
13
>>> short_stats['f_cdef']
15
>>> short_stats['f_cpdef']
17
>>> short_stats['f_inline']
19
>>> short_stats['f_inline_prof']
21
>>> short_stats['f_noprof']
22
Traceback (most recent call last):
25
>>> short_stats['f_raise']
28
>>> short_stats['withgil_prof']
30
>>> short_stats['withgil_noprof']
31
Traceback (most recent call last):
33
KeyError: 'withgil_noprof'
35
>>> short_stats['nogil_prof']
36
Traceback (most recent call last):
38
KeyError: 'nogil_prof'
39
>>> short_stats['nogil_noprof']
40
Traceback (most recent call last):
42
KeyError: 'nogil_noprof'
44
>>> short_stats['m_def']
46
>>> short_stats['m_cdef']
48
>>> short_stats['m_cpdef']
52
... os.unlink(statsfile)
56
>>> sorted(callees(s, 'test_profile')) #doctest: +NORMALIZE_WHITESPACE
57
['f_cdef', 'f_cpdef', 'f_def',
58
'f_inline', 'f_inline_prof',
60
'm_cdef', 'm_cpdef', 'm_def',
63
>>> profile.runctx("test_generators()", locals(), globals(), statsfile)
64
>>> s = pstats.Stats(statsfile)
65
>>> short_stats = dict([(k[2], v[1]) for k,v in s.stats.items()])
66
>>> short_stats['generator']
69
>>> short_stats['generator_exception']
72
>>> short_stats['genexpr']
75
>>> sorted(callees(s, 'test_generators'))
76
['call_generator', 'call_generator_exception', 'generator_expr']
78
>>> list(callees(s, 'call_generator'))
81
>>> list(callees(s, 'generator'))
84
>>> list(callees(s, 'generator_exception'))
87
>>> list(callees(s, 'generator_expr'))
90
>>> list(callees(s, 'genexpr'))
93
>>> def python_generator():
96
>>> def call_python_generator():
97
... list(python_generator())
99
>>> profile.runctx("call_python_generator()", locals(), globals(), statsfile)
100
>>> python_stats = pstats.Stats(statsfile)
101
>>> python_stats_dict = dict([(k[2], v[1]) for k,v in python_stats.stats.items()])
103
>>> profile.runctx("call_generator()", locals(), globals(), statsfile)
104
>>> cython_stats = pstats.Stats(statsfile)
105
>>> cython_stats_dict = dict([(k[2], v[1]) for k,v in cython_stats.stats.items()])
107
>>> python_stats_dict['python_generator'] == cython_stats_dict['generator'] \
108
or (python_stats_dict['python_generator'], cython_stats_dict['generator'])
112
... os.unlink(statsfile)
119
def callees(pstats, target_caller):
120
pstats.calc_callees()
121
for (_, _, caller), callees in pstats.all_callees.items():
122
if caller == target_caller:
123
for (file, line, callee) in callees.keys():
127
def test_profile(long N):
130
for i from 0 <= i < N:
134
n += (<object>f_cpdef)(i)
136
n += f_inline_prof(i)
140
n += withgil_noprof(i)
143
n += (<object>a).m_def(i)
145
n += (<object>a).m_cpdef(i)
156
cdef long f_cdef(long a):
159
cpdef long f_cpdef(long a):
162
cdef inline long f_inline(long a):
166
cdef inline long f_inline_prof(long a):
169
@cython.profile(False)
170
cdef int f_noprof(long a):
173
cdef long f_raise(long) except -2:
176
@cython.profile(False)
177
cdef int withgil_noprof(long a) with gil:
180
cdef int withgil_prof(long a) with gil:
183
@cython.profile(False)
184
cdef int nogil_noprof(long a) nogil:
187
cdef int nogil_prof(long a) nogil:
191
def m_def(self, long a):
193
cpdef m_cpdef(self, long a):
195
cdef m_cdef(self, long a):
198
def test_generators():
200
call_generator_exception()
210
def call_generator_exception():
212
list(generator_exception())
216
def generator_exception():
221
e = (x for x in range(10))