Cython:将字符值转换为字符串的最快方法
Cython: Fastest way to convert char value to string
我需要尽快将多个字符值转换为相应 ASCII 字符的字符串。这是一个玩具示例。我希望从python环境调用的H()函数返回str 'aaa'。
from libcpp.string cimport string
cdef string G():
return chr(97)
def H():
cdef string s
s.append(G())
s.append(G())
s.append(G())
return s
我相信,这不是最佳变体,因为它使用 python 函数 ord(),它将 97 框入 python 对象,然后返回 char,将其框入另一个 python 对象 str,最后将其转换为 c++ 字符串。如何更快地进行转换?
找到了!
<string>chr(i)
可以替换为
string(1, <char>i)
以下是新变体的示例:
cdef string G():
return string(1,<char>97)
def H():
cdef string s
s.append(G())
s.append(G())
s.append(G())
return s
新变体的工作速度提高了 2 倍。
相关文章:
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 具有字符串化的可变参数宏的现代/通用方法
- 接收字符串并使用它来调用方法C++
- 如何分隔字符串并将标记传递给方法
- 是否有通用方法可以找到任何以 null 结尾的字符串的长度?
- 当映射包含字符串向量作为值时,从值中获取键的有效方法
- 在 C++11 中字符串化变量名称的替代方法
- 连接和压缩标准::vector<std::字符串的最佳方法>
- Esp8266 & Nodemcu:返回请求字符串的方法
- 将位字符串转储到二进制文件的最佳方法是什么
- 有没有一种 STL 方法可以找到字符串的所有排列,给出一个以 C++ 为单位的大小?
- 如何将字符串从 C++/CLI 方法返回到调用它的非托管C++
- 返回混合 \ 和 / 的文件系统路径字符串方法
- VS 链接器失败,标准::字符串方法出现"object already exists"错误
- Lua:为自定义用户数据提供一个字符串方法
- 语法错误字符串方法C++
- 返回原始子字符串的子字符串方法
- 为什么我不能在C++中使用这种方法与"char"而不是"字符串"方法?
- 如何使c++字符串函数的行为像Java和c#字符串方法