热线电话:13121318867

登录
2019-03-01 阅读量: 672
python如何区分factorial函数和gamma函数

伽玛值也可以使用factorial(x-1),但用gamma()是因为,如果我们比较两个函数来实现类似的任务,gamma()提供更好的性能。

代码#:比较factorial()gamma()

# Python code to demonstrate

# factorial() vs gamma()

import math

import time

# initializing argument

gamma_var = 6

# checking performance

# gamma() vs factorial()

start_fact = time.time()

res_fact = math.factorial(gamma_var-1)

print ("The gamma value using factorial is : "

+ str(res_fact))

print ("The time taken to compute is : "

+ str(time.time() - start_fact))

print ('\n')

start_gamma = time.time()

res_gamma = math.gamma(gamma_var)

print ("The gamma value using gamma() is : "

+ str(res_gamma))

print ("The time taken to compute is : "

+ str(time.time() - start_gamma))

输出:

The gamma value using factorial is : 120
The time taken to compute is : 9.059906005859375e-06

The gamma value using gamma() is : 120.0
The time taken to compute is : 5.245208740234375e-06
17.9321
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子