cul*_*rón 2 python floating-point binary-data
根据 timeit 统计,下面是我的程序中花费最多时间的一段代码。将 [-1.0, 1.0] 区间内的浮点数转换为无符号整数 [0, 2**32] 是一个脏函数。我怎样才能加速floatToInt
?
piece = []
rng = range(32)
for i in rng:
piece.append(1.0/2**i)
def floatToInt(x):
n = x + 1.0
res = 0
for i in rng:
if n >= piece[i]:
res += 2**(31-i)
n -= piece[i]
return res
Run Code Online (Sandbox Code Playgroud)
你尝试过明显的吗?
def floatToInt(x):
return int((x+1.0) * (2**31))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1187 次 |
最近记录: |