3

I am new in Python and I am trying to get the elapsed time of the execution.I want to print the start time end time and elapsed time. But it's not working as I wish. Here's a program that contains an option menu and I want to print the time of execution.

from tkinter import *
from timeit import default_timer as timer
class Sum:
    def __init__(self,name,firstVariable,secondVariable):
        self.name=name
        self.firstVariable=firstVariable
        self.secondVariable=secondVariable
        sumList.append(self)
        sumNames.append(name)
def getSum():
   start=timer()
   for s in sumList:
        if s.name==selectedSum.get():
            sum=s.firstVariable+s.secondVariable
            age.set(sum)
   end=timer()
   elapsedTime=end-start
   print(start)
   print(end)
   print(elapsedTime)
mainWindow=Tk()
mainWindow.title("Sum")
mainWindow.resizable(width=FALSE,height=FALSE)
mainWindow.geometry("500x400")
sumList=[]
sumNames=[]
Sum("13+12",13,12)
Sum("17+11",17,11)
Sum("20+10",20,10)
selectedSum=StringVar()
selectedSum.set(sumNames[0])
sumMenu=OptionMenu(mainWindow,selectedSum,*sumNames)
sumMenu.pack()
selectBtn=Button(mainWindow,text="select",command=getSum)
selectBtn.pack()
age=IntVar()
ageLbl=Label(mainWindow,textvariable=age)
ageLbl.pack()
mainWindow.mainloop()
stovfl
  • 14,998
  • 7
  • 24
  • 51
Arsaa
  • 87
  • 1
  • 1
  • 4

1 Answers1

0

Try modifying this part like so:

from timeit import timeit

def getSum():
   timer = """
   for s in sumList:
        if s.name==selectedSum.get():
            sum=s.firstVariable+s.secondVariable
            age.set(sum)
   """
   elapsed_time = timeit(timer, number = 1000)/1000
   print(elapsed_time)
NotAName
  • 3,821
  • 2
  • 29
  • 44