我正在尝试使用计时器来查看每个功能有多快。我的代码可以工作,但是运行了很多次后,我得到的结果略有不同,并且想知道我是否正确实现了功能。我只会去第10个斐波那契数,即55,以进行测试。每次我运行“ A”选项时,clockTime()函数都会返回一个比以前稍大的数字。任何想法都将不胜感激。
import math
import time
#create a time variable
start_time = time.time()
#create the golden Ratio formula
golden_ratio = (1 + math.sqrt(5)) / 2
#the runtime function
def clockTime():
print("\nrun time: " + str(time.time()-start_time))
#the golden ration function
def fibGen(num):
for number in range(0,num+1):
val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
print('{i:3}: {v:3}'.format(i=number, v=round(val)))
#the find element < Max number function
def elemFib(num):
for number in range(0,num+1):
val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
if val < num:
print('Fib({}): {}'.format(number, round(val)))
#Pythonic way
def pythonic():
a, b = 0,1
while a < 57:
print(a, sep=" ", end="\n")
a, b = b, a+b
#display the Main Menu
def dispMenu():
print('---------------------Fibonacci Series ------------------\n')
print('(A) Print Fibonacci numbers to the nth term')
print('(B) Print Fibonacci numbers until element is less than Max number')
print('(C) pythonic print')
print('(Q) Quit the program\n')
def main():
# set boolean control variable for loop
loop = True
#Create while loop for menu
while loop:
#Display the menu
dispMenu()
#Get user's input
choice = (input('Please make a selection: '))
#Perform the selected action
if choice.upper() == 'A':
num = int(input("How many Fibonacci numbers should I print? "))
fibGen(num)
clockTime()
elif choice.upper() == 'B':
num = int(input("the element should be less than? "))
elemFib(num)
clockTime()
elif choice.upper() =='C':
pythonic()
clockTime()
elif choice.upper() == 'Q':
print('\nExiting program, Thank you and Goodbye')
loop = False
else:
print('\nInvalid selection, try again\n')
main()
答案 0 :(得分:2)
问题是您在程序开始时而不是在运行要计时的函数之前初始化了start_time
。您要添加以前的运行时间以及用户阅读说明和做出决定等所花费的时间。这是对代码的重做,应该可以完成您想要的事情:
import math
import time
# create the golden Ratio formula
golden_ratio = (1 + math.sqrt(5)) / 2
# the runtime function
def clockTime(start_time):
print("\nrun time:", time.time() - start_time)
# the golden ration function
def fibGen(num):
for number in range(num+1):
val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
print('{i:3}: {v:3}'.format(i=number, v=round(val)))
# the find element < Max number function
def elemFib(num):
for number in range(num + 1):
val = (golden_ratio**number - (1 - golden_ratio)**number) / math.sqrt(5)
if val < num:
print('Fib({}): {}'.format(number, round(val)))
# Pythonic way
def pythonic():
a, b = 0, 1
while a < 57:
print(a, sep=" ", end="\n")
a, b = b, a + b
# display the Main Menu
def dispMenu():
print('---------------------Fibonacci Series ------------------\n')
print('(A) Print Fibonacci numbers to the nth term')
print('(B) Print Fibonacci numbers until element is less than Max number')
print('(C) pythonic print')
print('(Q) Quit the program\n')
def main():
# set boolean control variable for loop
loop = True
# Create while loop for menu
while loop:
# Display the menu
dispMenu()
# Get user's input
choice = input('Please make a selection: ').upper()
# Perform the selected action
if choice == 'A':
num = int(input("How many Fibonacci numbers should I print? "))
start_time = time.time()
fibGen(num)
clockTime(start_time)
elif choice == 'B':
num = int(input("the element should be less than? "))
start_time = time.time()
elemFib(num)
clockTime(start_time)
elif choice == 'C':
start_time = time.time()
pythonic()
clockTime(start_time)
elif choice == 'Q':
print('\nExiting program, Thank you and Goodbye')
loop = False
else:
print('\nInvalid selection, try again\n')
main()