首先,我无法在我的系统上重现22s(Intel Nehalem,64位Ubuntu,Python 2.6.5).
以下需要1.4s(这基本上是你的代码,我填写了一些空白):
import struct
class UI(object):
def __init__(self,string):
self.string = string
def UI32(self):
tmp = self.string[:4]
self.string = self.string[4:]
return struct.unpack(">I",tmp)[0]
U = UI('0' * 240000)
for i in range(60000):
test = U.UI32()
Run Code Online (Sandbox Code Playgroud)
现在,这里有几个明显的低效率,特别是在周围self.string.
我已经重写了你的代码:
import struct
class UI(object):
def __init__(self,string):
fmt = '>%dI' % (len(string) / 4)
self.ints = struct.unpack(fmt, string)
def __iter__(self):
return iter(self.ints)
U = UI('0' * 240000)
count = 0
for test in U:
count += 1
print count
Run Code Online (Sandbox Code Playgroud)
在同一台机器上它现在需要0.025秒.
| 归档时间: |
|
| 查看次数: |
1461 次 |
| 最近记录: |