Python从整数中删除N个最右数的最快方法

问题描述

在python 整数中删除N最右数字的最快方法是什么?

这是我的一些代码:

def f_int(dividend,n):
    return int(dividend/(10 ** n))

def f_str_to_int(dividend,n):
    return int(str(dividend)[:-n])

如果我们不在乎输出是否在int的str上,则可以跳过def_f_str_to_int()中的int():

def f_str(dividend,n):
    return str(dividend)[:-n]

我们还可以通过以十次方的形式询问输入来提高速度:

divisor_in_power_of_ten = 10 ** n #outside function run time
def f_int_hack (dividend,divisor_in_power_of_ten):
    return int(dividend/(divisor_in_power_of_ten))

我的问题是,是否有更快的方法(也许使用位操作)?我需要对其进行优化,因为它将用作实时Web的一部分。注意:仅限于使用python,而不是JS或Cython(可以在JS和Cython中扩展答案,但这不是主要问题)。

这是我的结果:

FUNCTION: f_int_hack Used 200170 times
        MEDIAN 1.1000000004202093e-06
        MEAN   1.5179877104460484e-06
        STDEV  3.600025074234889e-05
FUNCTION: f_int Used 199722 times
        MEDIAN 1.8999999999991246e-06
        MEAN   2.420203582980709e-06
        STDEV  3.482858342790541e-05
FUNCTION: f_str Used 200132 times
        MEDIAN 1.4999999997655777e-06
        MEAN   1.7462234924949252e-06
        STDEV  1.4733864640549157e-05
FUNCTION: f_str_to_int Used 199639 times
        MEDIAN 2.000000000279556e-06
        MEAN   2.751038624717222e-06
        STDEV  6.383386278143267e-05

编辑: 基准测试的功能(相应地进行编辑,因为我已经对其进行了一些编辑):

import time
import random
import statistics

def benchmark(functions,iteration,*args):
    times = {f.__name__: [] for f in functions}
    
    for i in range(iteration):
        func = random.choice(functions)
        t0 = time.perf_counter()
        func(*args)
        t1 = time.perf_counter()
        times[func.__name__].append(t1 - t0)
    
    for name,numbers in times.items():
        print('FUNCTION:',name,'Used',len(numbers),'times')
        print('\tMEDIAN',statistics.median(numbers))
        print('\tMEAN  ',statistics.mean(numbers))
        print('\tSTDEV ',statistics.stdev(numbers))

if __name__=="__main__":
    # Variables
    divident = 12345600000
    n = 3
    iteration = 1000000

    # The functions to compare
    def f_int(divident,n):
        return int(divident/(10 ** n))

    def f_str_to_int(divident,n):
        return int(str(divident)[:-n])

    functions = f_int,f_str_to_int
    benchmark(functions,divident,n)

更多说明:输入为 python整数。其实,我并不关心输出格式(无论是str还是int),但让我们先将int输出,并将str作为“奖励问题”。

编辑: 来自评论部分a//b

FUNCTION: f_int_double_slash Used 166028 times
        MEDIAN 1.2000000002565514e-06
        MEAN   1.4399938564575845e-06
        STDEV  1.2767417156171526e-05

是的,它比int(a / b)更快

编辑: 从评论来看,如果我们接受浮动,最快的方法是:

def f_float_hack(dividend,divisor_in_power_of_ten):
    return dividend//divisor_in_power_of_ten

结果:

FUNCTION: f_float_hack Used 142983 times
        MEDIAN 7.000000001866624e-07
        MEAN   9.574040270508322e-07
        STDEV  3.725603760159355e-05

使用10的预计算能力提高int双斜杠:

FUNCTION: f_int_double_slash_hack Used 143082 times
        MEDIAN 7.999999995789153e-07
        MEAN   1.1596266476572136e-06
        STDEV  4.9442788346866335e-05

当前结果:float_hack是最快的(如果我们接受float)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...