rig*_*cky 4 python string cython python-3.x
When defining a variable type that will hold a string in Cython + Python 3, I can use (at least):
cdef char* mystring = "foo"
cdef str mystring = "foo"
cdef bytes mystring = "foo"
Run Code Online (Sandbox Code Playgroud)
The documentation page on strings is unclear on this -- it mostly gives examples using char* and bytes, and frankly I'm having a lot of difficulty understanding it.
In my case the strings will be coming from a Python3 program and are assumed to be unicode. They will be used as dict keys and function arguments, but I will do no further manipulation on them. Needless to say I am trying to maximize speed.
This question suggests that under Python2.7 and without Unicode, typing as str makes string manipulation code run SLOWER than with no typing at all. (But that's not necessarily relevant here since I won't be doing much string manipulation.)
What are the advantages and disadvantages of each of these options?
如果没有对特定类型进行进一步处理,则最好不要最快地根本不键入它们,这意味着它们被视为通用PyObject *。
该str类型是一种特殊情况,这意味着bytes在Python 2和unicodePython 3上。
str类型的特殊之处在于它是Python 2中的字节字符串和Python 3中的Unicode字符串
因此,将字符串键入为as str并将其作为unicode处理的代码将在python 2上打破,其中strmeans bytes。
仅在将字符串转换为C char*或C ++ 时才需要键入字符串std::string。在那里,您将str用来处理py2 / py3的兼容性,以及用于将字节和unicode相互转换的辅助函数,以便能够转换为char*或std::string。
键入字符串是为了与C / C ++互操作,而不是为了提高速度。Cython会自动将一个bytes字符串转换char*为一个字符串,而不进行复制,例如,当它看到类型是cdef char* c_string = b_string[:b_len]where时。b_stringbytes
OTOH,如果在不使用该类型的情况下键入字符串,则Cython会在不需要时从对象转换为字节/ Unicode,这会导致开销。
可以在Pyx_PyObject_AsString,Pyx_PyUnicode_FromString等人生成的C代码中看到。
一般而言,这也是正确的-经验法则是,如果不需要进一步处理/转换的特定类型,则最好根本不要键入它。python中的所有内容都是一个对象,因此键入会将常规用途转换PyObject*为更具体的用途。