cython

Форк
0
/
pstats_profile_test_pycfunc.pyx 
222 строки · 5.1 Кб
1
# tag: pstats
2
# cython: profile = True
3
# cython: binding = False
4

5
__doc__ = u"""
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']
12
    100
13
    >>> short_stats['f_cdef']
14
    100
15
    >>> short_stats['f_cpdef']
16
    200
17
    >>> short_stats['f_inline']
18
    100
19
    >>> short_stats['f_inline_prof']
20
    100
21
    >>> short_stats['f_noprof']
22
    Traceback (most recent call last):
23
    ...
24
    KeyError: 'f_noprof'
25
    >>> short_stats['f_raise']
26
    100
27

28
    >>> short_stats['withgil_prof']
29
    100
30
    >>> short_stats['withgil_noprof']
31
    Traceback (most recent call last):
32
    ...
33
    KeyError: 'withgil_noprof'
34

35
    >>> short_stats['nogil_prof']
36
    Traceback (most recent call last):
37
    ...
38
    KeyError: 'nogil_prof'
39
    >>> short_stats['nogil_noprof']
40
    Traceback (most recent call last):
41
    ...
42
    KeyError: 'nogil_noprof'
43

44
    >>> short_stats['m_def']
45
    200
46
    >>> short_stats['m_cdef']
47
    100
48
    >>> short_stats['m_cpdef']
49
    200
50

51
    >>> try:
52
    ...    os.unlink(statsfile)
53
    ... except:
54
    ...    pass
55

56
    >>> sorted(callees(s, 'test_profile'))  #doctest: +NORMALIZE_WHITESPACE
57
    ['f_cdef', 'f_cpdef', 'f_def',
58
     'f_inline', 'f_inline_prof',
59
     'f_raise',
60
     'm_cdef', 'm_cpdef', 'm_def',
61
     'withgil_prof']
62

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']
67
    3
68

69
    >>> short_stats['generator_exception']
70
    2
71

72
    >>> short_stats['genexpr']
73
    11
74

75
    >>> sorted(callees(s, 'test_generators'))
76
    ['call_generator', 'call_generator_exception', 'generator_expr']
77

78
    >>> list(callees(s, 'call_generator'))
79
    ['generator']
80

81
    >>> list(callees(s, 'generator'))
82
    []
83

84
    >>> list(callees(s, 'generator_exception'))
85
    []
86

87
    >>> list(callees(s, 'generator_expr'))
88
    ['genexpr']
89

90
    >>> list(callees(s, 'genexpr'))
91
    []
92

93
    >>> def python_generator():
94
    ...   yield 1
95
    ...   yield 2
96
    >>> def call_python_generator():
97
    ...   list(python_generator())
98

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()])
102

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()])
106

107
    >>> python_stats_dict['python_generator'] == cython_stats_dict['generator']  \
108
            or  (python_stats_dict['python_generator'], cython_stats_dict['generator'])
109
    True
110

111
    >>> try:
112
    ...    os.unlink(statsfile)
113
    ... except:
114
    ...    pass
115
"""
116

117
cimport cython
118

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():
124
            if 'pyx' in file:
125
                yield callee
126

127
def test_profile(long N):
128
    cdef long i, n = 0
129
    cdef A a = A()
130
    for i from 0 <= i < N:
131
        n += f_def(i)
132
        n += f_cdef(i)
133
        n += f_cpdef(i)
134
        n += (<object>f_cpdef)(i)
135
        n += f_inline(i)
136
        n += f_inline_prof(i)
137
        n += f_noprof(i)
138
        n += nogil_noprof(i)
139
        n += nogil_prof(i)
140
        n += withgil_noprof(i)
141
        n += withgil_prof(i)
142
        n += a.m_def(i)
143
        n += (<object>a).m_def(i)
144
        n += a.m_cpdef(i)
145
        n += (<object>a).m_cpdef(i)
146
        n += a.m_cdef(i)
147
        try:
148
            n += f_raise(i+2)
149
        except RuntimeError:
150
            pass
151
    return n
152

153
def f_def(long a):
154
    return a
155

156
cdef long f_cdef(long a):
157
    return a
158

159
cpdef long f_cpdef(long a):
160
    return a
161

162
cdef inline long f_inline(long a):
163
    return a
164

165
@cython.profile(True)
166
cdef inline long f_inline_prof(long a):
167
    return a
168

169
@cython.profile(False)
170
cdef int f_noprof(long a):
171
    return a
172

173
cdef long f_raise(long) except -2:
174
    raise RuntimeError
175

176
@cython.profile(False)
177
cdef int withgil_noprof(long a) with gil:
178
    return (a)
179
@cython.profile(True)
180
cdef int withgil_prof(long a) with gil:
181
    return (a)
182

183
@cython.profile(False)
184
cdef int nogil_noprof(long a) nogil:
185
    return a
186
@cython.profile(True)
187
cdef int nogil_prof(long a) nogil:
188
    return a
189

190
cdef class A(object):
191
    def m_def(self, long a):
192
        return a
193
    cpdef m_cpdef(self, long a):
194
        return a
195
    cdef m_cdef(self, long a):
196
        return a
197

198
def test_generators():
199
    call_generator()
200
    call_generator_exception()
201
    generator_expr()
202

203
def call_generator():
204
    list(generator())
205

206
def generator():
207
    yield 1
208
    yield 2
209

210
def call_generator_exception():
211
    try:
212
        list(generator_exception())
213
    except ValueError:
214
        pass
215

216
def generator_exception():
217
    yield 1
218
    raise ValueError(2)
219

220
def generator_expr():
221
    e = (x for x in range(10))
222
    return sum(e)
223

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.