簡體   English   中英

Python:是否有可能使這種尾遞歸階乘更快?

[英]Python: Is it possible to make this tail-recursion factorial even faster?

我正在為涉及組合學的應用程序制作實用程序類。

我做了兩個因子函數,它的尾遞歸。 第二個在計算組合數時會提高很多性能(我認為在英語中它們被稱為n選擇k ,我使用C(n,k)表示法並且也使用了nCk函數)。

所以我測量了時間,我實現了目標。 我的新組合函數比新組合快得多。

但是當進入階乘函數時,我發現我的新因子函數'f'比第一個'事實'慢一點。

有沒有辦法讓這個'f'功能在性能上與'事實'一樣好?

您可以在底部看到我的機器中的主要測試執行和結果。

源代碼

class Utils(object):
    """
    Class with useful functions
    """

    @staticmethod
    def fact(i, _current_factorial=1):
        '''
        The factorial function
        :param i: the number for calculating the factorial
        :param _current_factorial: for internal use, it is the acumulated product
        :return the result
        '''
        if i == 1:
            return _current_factorial
        else:
            return Utils.fact(i - 1, _current_factorial * i)

    @staticmethod
    def f(i, k=None, _current_factorial=1):
        '''
        The factorial function
        If k is given, it calculates the factorial but only with
        the k-th first numbers.
        Example:
            f(6) = 6 · 5 · 4 · 3 · 2 · 1
            f(6, 2) = 6 · 5
            f(6, 3) = 6 · 5 · 4
            f(9, 4) = 9 · 8 · 7 · 6
        :param i: the number for calculating the factorial
        :param k: optional, the number for calculating the factorial
        :param _current_factorial: for internal use, it is the acumulated product
        :return the result
        '''
        if k is None:
            k = i
        if i == 1 or k == 0:
            return _current_factorial
        else:
            return Utils.f(i - 1, k - 1, _current_factorial=_current_factorial * i)

    @staticmethod
    def C(n, k=1):
        '''
        Statistical combinations 'nCk'
        :param n: number of elements to be combined
        :param k: number of elements to be taken for each combination
        :return: the 'n choose k' mathematical result
        '''
        return Utils.fact(n) / (Utils.fact(k) * Utils.fact(n - k))

    @staticmethod
    def nCk(n, k=1):
        '''
        Statistical combinations 'nCk'
        :param n: number of elements to be combined
        :param k: number of elements to be taken for each combination
        :return: the 'n choose k' mathematical result
        '''
        return Utils.f(n, k) / Utils.f(k)



if __name__ == "__main__":
    NUM_TEST_ITERATIONS = 7500
    MAX_NUMBER_WITHOUT_STACKOVERFLOW = 998

    import time

    start = time.process_time()
    for i in range(NUM_TEST_ITERATIONS): Utils.f(MAX_NUMBER_WITHOUT_STACKOVERFLOW)
    elapsed = time.process_time() - start
    print('Function {} took {} to get result {}'.format('f', elapsed, Utils.f(MAX_NUMBER_WITHOUT_STACKOVERFLOW)))

    start = time.process_time()
    for i in range(NUM_TEST_ITERATIONS): Utils.fact(MAX_NUMBER_WITHOUT_STACKOVERFLOW)
    elapsed = time.process_time() - start
    print('Function {} took {} to get result {}'.format('fact', elapsed, Utils.fact(MAX_NUMBER_WITHOUT_STACKOVERFLOW)))

    start = time.process_time()
    for i in range(NUM_TEST_ITERATIONS): Utils.nCk(997, 4)
    elapsed = time.process_time() - start
    print('Function {} took {} to get result {}'.format('nCk', elapsed, Utils.nCk(997, 4)))

    start = time.process_time()
    for i in range(NUM_TEST_ITERATIONS): Utils.C(997, 4)
    elapsed = time.process_time() - start
    print('Function {} took {} to get result {}'.format('C', elapsed, Utils.C(997, 4)))

在屏幕上

Function f    took 7.854962716 to get result 402790050127220994538240674597601587306681545756471103647447357787726238637266286878923131618587992793273261872069265323955622495490298857759082912582527118115540044131204964883707335062250983503282788739735011132006982444941985587005283378024520811868262149587473961298417598644470253901751728741217850740576532267700213398722681144219777186300562980454804151705133780356968636433830499319610818197341194914502752560687555393768328059805942027406941465687273867068997087966263572003396240643925156715326363340141498803019187935545221092440752778256846166934103235684110346477890399179387387649332483510852680658363147783651821986351375529220618900164975188281042287183543472177292257232652561904125692525097177999332518635447000616452999984030739715318219169707323799647375797687367013258203364129482891089991376819307292252205524626349705261864003453853589870620758596211518646408335184218571196396412300835983314926628732700876798309217005024417595709904449706930796337798861753941902125964936412501007284147114260935633196107341423863071231385166055949914432695939611227990169338248027939843597628903525815803809004448863145157344706452445088044626373001304259830129153477630812429640105937974761667785045203987508259776060285826091261745049275419393680613675366264232715305430889216384611069135662432391043725998805881663054913091981633842006354699525518784828195856033032645477338126512662942408363494651203239333321502114252811411713148843370594801145777575035630312885989779863888320759224882127141544366251503974910100721650673810303577074640154112833393047276025799811224571534249672518380758145683914398263952929391318702517417558325636082722982882372594816582486826728614633199726211273072775131325222240100140952842572490801822994224069971613534603487874996852498623584383106014533830650022411053668508165547838962087111297947300444414551980512439088964301520461155436870989509667681805149977993044444138428582065142787356455528681114392680950815418208072393532616122339434437034424287842119316058881129887474239992336556764337968538036861949918847009763612475872782742568849805927378373244946190707168428807837146267156243185213724364546701100557714520462335084082176431173346929330394071476071813598759588818954312394234331327700224455015871775476100371615031940945098788894828812648426365776746774528000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Function fact took 6.757415364 to get result 402790050127220994538240674597601587306681545756471103647447357787726238637266286878923131618587992793273261872069265323955622495490298857759082912582527118115540044131204964883707335062250983503282788739735011132006982444941985587005283378024520811868262149587473961298417598644470253901751728741217850740576532267700213398722681144219777186300562980454804151705133780356968636433830499319610818197341194914502752560687555393768328059805942027406941465687273867068997087966263572003396240643925156715326363340141498803019187935545221092440752778256846166934103235684110346477890399179387387649332483510852680658363147783651821986351375529220618900164975188281042287183543472177292257232652561904125692525097177999332518635447000616452999984030739715318219169707323799647375797687367013258203364129482891089991376819307292252205524626349705261864003453853589870620758596211518646408335184218571196396412300835983314926628732700876798309217005024417595709904449706930796337798861753941902125964936412501007284147114260935633196107341423863071231385166055949914432695939611227990169338248027939843597628903525815803809004448863145157344706452445088044626373001304259830129153477630812429640105937974761667785045203987508259776060285826091261745049275419393680613675366264232715305430889216384611069135662432391043725998805881663054913091981633842006354699525518784828195856033032645477338126512662942408363494651203239333321502114252811411713148843370594801145777575035630312885989779863888320759224882127141544366251503974910100721650673810303577074640154112833393047276025799811224571534249672518380758145683914398263952929391318702517417558325636082722982882372594816582486826728614633199726211273072775131325222240100140952842572490801822994224069971613534603487874996852498623584383106014533830650022411053668508165547838962087111297947300444414551980512439088964301520461155436870989509667681805149977993044444138428582065142787356455528681114392680950815418208072393532616122339434437034424287842119316058881129887474239992336556764337968538036861949918847009763612475872782742568849805927378373244946190707168428807837146267156243185213724364546701100557714520462335084082176431173346929330394071476071813598759588818954312394234331327700224455015871775476100371615031940945098788894828812648426365776746774528000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Function nCk took 0.021283503999999454 to get result 40921610765.0
Function C   took 13.261998684000002   to get result 40921610765.0

我最后刪除了類代碼並使用了math.factorial,但我用'fact'名稱導入了它。

我還使用源代碼文檔中解釋的公式中的快捷方式

這比原來要好得多。 謝謝

源代碼 (為簡潔省略主要內容):

# Importing math.factorial with shorter name
from math import factorial as fact
from functools import reduce
from operator import mul


def C(n, k=1):
    '''
    Statistical combinations 'nCk'
    :param n: number of elements to be combined
    :param k: number of elements to be taken for each combination
    :return: the 'n choose k' mathematical result
    '''
    '''
        Original formula: fact(n) / (fact(k) * fact(n-k))

        Performance shortcut (about 40% faster): 
            1.- Calculates the numerator as the product over the first k-th elements of fact(n)
            Let's call this f'(n,k), we store it in fnkProduct variable
            Example:
                n = 6, k = 2 => f'(n,k) =   30 = 6 · 5
                n = 6, k = 3 => f'(n,k) =  120 = 6 · 5 · 4
                n = 9, k = 4 => f'(n,k) = 3024 = 9 · 8 · 7 · 6
            2.- Then divide it by fact(k)

        New formula: f'(n,k) / fact(k)

        '''
    # C(n,k) == C(n,n-k), but the second is best when k is much bigger
    k = n - k if k > (n // 2) else k
    fnkProduct = reduce(mul, range(n, n - k, -1), 1)
    return fnkProduct // fact(k)

在屏幕上 (這是我在決定1500萬次迭代之前做的最后一次比較)

Function C took 0.01638609299999999 to get result 40921610765.0
Function nCk took 0.016534930000000003 to get result 40921610765.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM