0

I'm trying to use a timer to see how fast each function is. My codes work but after running it many times I get slightly different results and wonder if I'm implementing function correctly. I am only going to the 10th Fibonacci number which is 55 for testing. Each time I run the "A" option the clockTime() function returns a slightly larger number than before. Any thoughts would be appreciated.

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()
CelestialSky
  • 57
  • 2
  • 14

1 Answers1

2

The problem is you initialized start_time at the start of the program rather than right before you ran the function to be timed. You were adding in the times of previous runs as well as the time the user took to read the instructions and make a decision, etc. Here's a rework of your code that should do what you want:

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81