python - Using time.time() to time a function often return 0 seconds -
i have time implementation did of algorithm in 1 of classes, , using time.time() function so. after implementing it, have run algorithm on number of data files contains small , bigger data sets in order formally analyse complexity.
unfortunately, on small data sets, runtime of 0 seconds if precision of 0.000000000000000001 function when looking @ runtimes of bigger data sets , cannot believe takes less on smaller data sets.
my question is: there problem using function (and if so, there function can use has better precision)? or doing wrong?
here code if ever need it:
import sys, time import random utility import parsesystemarguments, printresults ... def main(ville): start = time.time() solution = dynamique(ville) # algorithm implementation end = time.time() return (end - start, solution) if __name__ == "__main__": sys.argv.insert(1, "-a") sys.argv.insert(2, "3") (algonumber, ville, printlist) = parsesystemarguments() (algotime, solution) = main(ville) printresults(algotime, solution, printlist) the printresults function:
def printresults(time, solution, printlist=true): print ("temps d'execution = " + str(time) + "s") if printlist: print (solution)
don't confuse resolution of system time resolution of floating point number. time resolution on computer frequent system clock updated. how system clock updated varies machine machine, ensure see difference time, need make sure executes millisecond or more. try putting loop this:
start = time.time() k = 100000 in range(k) solution = dynamique(ville) end = time.time() return ((end - start)/k, solution) in final tally, need divide number of loop iterations know how long code runs once through. may need increase k measure of execution time, or may need decrease if computer running in loop long time.
Comments
Post a Comment