实际上,这是一个简单的问题:您有 10 亿 (1e+9) 个无符号 32 位整数作为十进制 ASCII 字符串存储在 TSV(制表符分隔值)文件中。转换使用int()
与处理相同数据集的其他工具相比,速度非常慢。为什么?更重要的是:如何让它更快?
因此问题是:在Python中将字符串转换为整数的最快方法是什么?
我真正想到的是一些半隐藏的 Python 功能,可以(滥用)用于此目的,与 Guido 的使用不同array.array
in his 《优化轶事》 http://www.python.org/doc/essays/list2str/.
样本数据(制表符扩展为空格)
38262904 "pfv" 2002-11-15T00:37:20+00:00
12311231 "tnealzref" 2008-01-21T20:46:51+00:00
26783384 "hayb" 2004-02-14T20:43:45+00:00
812874 "qevzasdfvnp" 2005-01-11T00:29:46+00:00
22312733 "bdumtddyasb" 2009-01-17T20:41:04+00:00
读取数据所花费的时间在这里无关紧要,处理数据才是瓶颈。
微基准测试
以下所有语言都是解释性语言。主机运行 64 位 Linux。
Python 2.6.2 和 IPython 0.9.1,每秒约 214k 次转换 (100%):
In [1]: strings = map(str, range(int(1e7)))
In [2]: %timeit map(int, strings);
10 loops, best of 3: 4.68 s per loop
REBOL 3.0 版本 2.100.76.4.2,~231kcps (108%):
>> strings: array n: to-integer 1e7 repeat i n [poke strings i mold (i - 1)]
== "9999999"
>> delta-time [map str strings [to integer! str]]
== 0:00:04.328675
REBOL 2.7.6.4.2(2008 年 3 月 15 日),~523kcps (261%):
正如约翰在评论中指出的那样,这个版本确实not构建一个转换后的整数列表,因此给出的速度比是相对于 Python 的 4.99s 运行时间for str in strings: int(str)
.
>> delta-time: func [c /local t] [t: now/time/precise do c now/time/precise - t]
>> strings: array n: to-integer 1e7 repeat i n [poke strings i mold (i - 1)]
== "9999999"
>> delta-time [foreach str strings [to integer! str]]
== 0:00:01.913193
KDB+ 2.6t 2009.04.15, ~2016kcps (944%):
q)strings:string til "i"$1e7
q)\t "I"$strings
496