相关疑难解决方法(0)

为什么Python 2.x中的math.factorial比3.x慢得多?

我在我的机器上得到以下结果:

Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.timeit('factorial(10000)', 'from math import factorial', number=100)
1.9785256226699202
>>>

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.timeit('factorial(10000)', 'from math import factorial', number=100)
9.403801111593792
>>>
Run Code Online (Sandbox Code Playgroud)

我认为这可能与int/long转换有关,但factorial(10000L)在2.7中没有任何更快.

python performance python-2.x factorial python-3.x

32
推荐指数
1
解决办法
4516
查看次数

是否可以在python中快速将非常大的int转换为字符串

我正在构建一个生成大量整数的加密程序。它看起来像这样:

a = plaintextOrd**bigNumber
Run Code Online (Sandbox Code Playgroud)

当我做

a = str(a)
Run Code Online (Sandbox Code Playgroud)

需要28分钟以上。

有没有办法比使用内置的 str() 函数更快地转换这样的整数?

我需要它是一个字符串的原因是因为这里的这个函数:

def divideStringIntoParts(parts,string):
    parts = int(parts)
    a = len(string)//parts

    new = []
    firstTime = True
    secondTime = True
    for i in range(parts):
        if firstTime:
            new.append(string[:a])
            firstTime = False
        elif secondTime:
            new.append(string[a:a+a])
            secondTime = False
        else:
            new.append(string[a*i:a*(i+1)])

    string2 = ""
    for i in new:
        for i in i:
            string2 += i

    if len(string2) - len(string) != 0:
        lettersNeeded = len(string) - len(string2)
        for i in range(lettersNeeded):
            new[-1] += string[len(string2) …
Run Code Online (Sandbox Code Playgroud)

python

6
推荐指数
1
解决办法
704
查看次数

标签 统计

python ×2

factorial ×1

performance ×1

python-2.x ×1

python-3.x ×1