- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为涉及组合学的应用程序制作实用程序类。
我做了两个尾递归的阶乘函数。第二个在计算组合数时提高了很多性能(我认为在英语中它们被称为n choose k,我使用 C(n,k) 表示法并制作了一个 nCk 函数)。
所以我测量了时间,并实现了我的目标。我的新组合函数比新函数快得多。
但是当谈到阶乘函数时,我发现我的新阶乘函数 'f' 比第一个 'fact' 慢一点。
有没有办法让这个“f”函数在性能上和“fact”一样好?
您可以在底部看到我机器中的主要测试执行和结果。
源代码
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
关于Python:是否有可能使这个尾递归阶乘更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369377/
好吧,我知道这个问题已经被问了无数次了。但是,对于我在谷歌搜索中似乎无法找到的问题,我还有一个小补充。 我当然不是 FFMPEG 的专家……我一直在使用 FFMPEG 的标准加速/减速模板,我正在使用
考虑这三个文档... [ { _id: "...", _rev: "...", title: "Foo", body: "...
我想知道访问我的全局变量的最快方法...它们只会在 Beta 测试阶段发生变化。在我们上线之前。从那时起,它们将永远不会改变。 我认为从 web.config 中获取内容会产生开销,而且编写 App.
这个问题在这里已经有了答案: 11 年前关闭。 Possible Duplicate: Is there a performance difference between BETWEEN and IN
我很想知道对通常作为查询目标的数字列进行分区是否有性能优势。目前我有一个包含约 5000 万条记录的物化 View 。当使用常规 b 树索引并按此数字列搜索时,我得到的成本为 7,查询结果大约需要 0
我需要编写一个库,它执行许多远程 HTTP 调用来获取内容。我可以按照描述做here ,但是有没有更好的方法(在性能方面)如何做到这一点?如果我按照示例中所述进行操作,我总是会创建一个 URL 对象,
该代码非常不言自明。只是有很多我需要独立随机化的范围。例如,范围('W1:W4')不应与范围('W5:W8')混淆,因此我不能只是随机化范围('W1:W80')。任何帮助或建议都会很棒!多谢。目前,代
我正在使用 ADT 模拟器。我在我的模拟器中使用默认的 Android 虚拟设备。我创建了一个版本 4.0.3。 问题 太慢了。有时我在尝试更改 fragment 时会收到加载点击。 我使用的代码是有
我正在尝试获取一个包含三个表中的信息的数组。结果应该是一个数组,我可以在其中循环遍历第一个表、第二个表中的相关行以及第三个表到第二个表中的相关行。目前,我有三个独立的 SQL 查询,然后将它们重组为一
我已经学会了两种在服务器上上传图像的方法(可能还有更多..)。 1) 创建 NSData 并将其添加到请求正文中 2)创建字节数组并像简单数组一样以json形式发送 1) 创建 NSData 并将其添
我有一个 UItextview,我可以在里面写入数据类,我可以在我的 View 中的任何地方提供数据,在 ViewDidAppear 函数中我传递了我的数据,但它有点慢。文本在 0.2-0.3 秒后出
如何为 discoverAllContactUserInfosWithCompletionHandler 创建优先级高于默认值的 CKOperation? 我找不到不使用 [[CKContainer
我在 unix 模块下编写了一个内核级函数,用于对系统负载进行采样。我在 clock.c 下的 clock() 中调用示例函数,以在每个时钟(例如,我的系统上每 10 毫秒)拍摄系统负载的快照。有没有
我正在制作一个应用程序,该应用程序将根据变量的值使用鼠标/键盘(宏)模拟操作。 这里有我制作的 de 扫描代码: void ReadMemory(int value){ DWORD p
我想知道在计算上调用嵌套在对象中的函数的最快方法是什么,所以我做了一个快速的 jsPerf.com 基准测试,其中我考虑了三种可能性——从数组中调用函数,从“核心”中调用函数对象和函数对象: var
我用 php 做了一个图像缩放器。调整图像大小时,它会缓存一个具有新尺寸的新 jpg 文件。下次您调用确切的 img.php?file=hello.jpg&size=400 时,它会检查是否已经创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Which is best for data store Struct/Classes? 考虑我有一个 Em
我正在尝试为多组列自动计算每行的平均分数。例如。一组列可以代表不同比例的项目。这些列也被系统地命名 (scale_itemnumber)。 例如,下面的虚拟数据框包含来自三个不同比例的项目。(可能会出
所以我知道散列图使用桶和散列码等等。根据我的经验,Java 哈希码并不小,但通常很大,所以我假设它没有在内部建立索引。除非哈希码质量很差导致桶长度和桶数量大致相等,否则 HashMap 比名称-> 值
假设我有一个非常缓慢和大的 for 循环。 如何将其拆分为多个线程以使其运行速度更快? for (int a = 0; a { slowMet
我是一名优秀的程序员,十分优秀!