I want to compare the processing speed of instance variables and function arguments.
I wrote the following code, but most of it took time to print.
How can I do a pure speed comparison?
import time class checkTime(): def __init__(self): self.a = 0 self.b = 0 self.c = 0 def printWithArgument(self,a,b,c): print(a+b+c) def printWithSelf(self): print(self.a+self.b+self.c) def getTime(self): printWithSelf_start = time.time() for i in range(0,100000): self.a = 1 self.b = 2 self.c = 3 self.printWithSelf() printWithSelf_elapsed_time = time.time() - printWithSelf_start printWithArgument_start = time.time() for i in range(0,100000): self.printWithArgument(1,2,3) printWithArgument_elapsed_time = time.time() - printWithArgument_start print ("printWithArgument_time:{0}".format(printWithArgument_elapsed_time) + "[sec]") print ("printWithSelf_time:{0}".format(printWithSelf_elapsed_time) + "[sec]") checkTime = checkTime() checkTime.getTime()