def get_times(denoms, n): time_file = open("times.csv", "w") for i in range(1, n + 1): t_zero = time() changeslow(denoms, i) time_file.write("{},{}\n".format(time() - t_zero, i)) time_file.close()
def main(): if not (len(sys.argv) > 0): print("You must supply a file for input.") exit(1) infilename = str(sys.argv[1]) outfilename = make_change_filename(infilename) list_denoms, amounts = read_input(infilename) with open(outfilename, "w") as outfile: for index, denoms in enumerate(list_denoms): coins = changeslow(denoms, amounts[index]) outfile.writelines("\n".join([ "Algorithm changedp:", " ".join([str(el) for el in denoms]), " ".join([str(el) for el in coins]), str(sum(coins)), "\n" ]))
def cointime(values,amount): intime = time.clock() coins = changeslow(values,amount) outime = time.clock() - intime return sum(coins),outime*1000
def experimentalAnalysis(nMax, stepSize, start): print "Generating array..." outfile = 'arr15.txt' f = open(outfile, 'w+') #All the different arrays and sizes, starting at 4, increasing to 15 #coinArray = [1, 5, 10, 25] #coinArray = [1, 5, 10, 20, 30] #coinArray = [1, 3, 6, 12, 18, 24] #coinArray = [1, 2, 6, 12, 24, 48, 60] #coinArray = [1, 4, 8, 12, 16, 20, 24, 28] #coinArray = [1, 3, 6, 9, 12, 18, 21, 24, 27] #coinArray = [1, 2, 5, 7, 10, 12, 15, 17, 20, 22] #coinArray = [1, 2, 6, 8, 10, 12, 14, 16, 18, 20, 22] #coinArray = [1, 2, 4, 7, 10, 12, 14, 17, 20, 23, 25, 28] #coinArray = [1, 3, 5, 8, 10, 13, 15, 18, 20, 23, 25, 28, 30] #coinArray = [1, 2, 5, 8, 10, 13, 15, 18, 20, 22, 24, 26, 28, 30] coinArray = [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30] #generate array of amounts testArray = [] for n in range(start, nMax+1, stepSize): testArray.append(n) #Tests each algorithms f.write("-----------Analysis for Algorithm ChangeSlow-----------\n") f.write("CoinAmt\tResult\tTime\n") for a in testArray: #test each amount stored in testArray if a <= 60: #Limit for recursive algo - above run times t0 = time() #are too high result = changeslow(coinArray, a) #call algorithm t1 = time() f.write("{0} {1} {2} \n".format(a,result[1],(t1-t0))) #writes time & results else: break if (t1 - t0 > 45): #Another limit to prevent long run times break print "Algo 1 done." #Next to blocks same as previous without the limits f.write("\n-----------Analysis for Algorithm ChangeGreedy-----------\n") f.write("CoinAmt\tResult\tTime\n") for a in testArray: t0 = time() result = changegreedy(coinArray, a) t1 = time() f.write("{0} {1} {2} \n".format(a,result[1],(t1-t0))) print "Algo 2 done." f.write("\n-----------Analysis for Algorithm ChangeDP-----------\n") f.write("CoinAmt\tResult\tTime\n") for a in testArray: t0 = time() result = changedp(coinArray, a) t1 = time() f.write("{0} {1} {2} \n".format(a,result[1],(t1-t0))) f.close()