我有以下Cython
代码:探查的用Cython代码
# cython: profile=True
import cProfile
from cython import parallel
from libc.stdio cimport FILE, fopen, fclose, fwrite, getline, printf
from libc.string cimport strlen
from libcpp.string cimport string
cdef extern from "stdio.h" nogil:
int mkstemp(char*);
cdef run_io(string obj):
cdef int i, dump
cdef size_t len_ = 0
cdef char* fname = "/tmp/tmpc_XXXXXX"
cdef char* nullchar = NULL
cdef char* line = NULL
cdef string content = b""
cdef FILE* cfile
for i in range(10000):
dump = mkstemp(fname)
cfile = fopen(fname, "wb")
fwrite(obj.data(), 1, obj.size(), cfile)
fclose(cfile)
cfile = fopen(fname, "rb")
while True:
if getline(&line, &len_, cfile) == -1:
break
else:
content.append(line)
fclose(cfile)
def run_test():
cdef string obj = b"abc\ndef"
cProfile.runctx("run_io(obj)", globals(), locals())
当我尝试从一个python3
控制台运行它,我得到的错误:
NameError: name 'run_io' is not defined
如果我在清晰度改变run_io
功能cdef
至def
,它的工作原理:
7 function calls in 2.400 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 2.400 2.400 <string>:1(<module>)
2 0.000 0.000 0.000 0.000 stringsource:13(__pyx_convert_string_from_py_std__in_string)
1 2.400 2.400 2.400 2.400 testc2.pyx:10(run_io)
1 0.000 0.000 2.400 2.400 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 2.400 2.400 {test.run_io}
但是,这并不是很丰富,因为我只看到了整个函数的总运行时间(我希望看到生成文件名,读取,写入等的部分运行时)。
因此,我有两个问题:
是否有可能剖析
Cython
功能(与cdef
定义)?如果是,如何?如何使分析更具信息性(即测量每个被调用函数花费的时间)?