results = [0,0,0] #Generate array with n random entries myArray = [] generateArray(myArray,n) # timeit clocking function time for Algorithm 1 # To save time, only run timings on Algorithm 1 up to n=1000 if n<1001: t=Timer(lambda: maxsub.maxSubarray1(myArray,results)) alg1time = t.timeit(number=1) # timeit clocking function time for Algorithm 2 # To save time, only run timings on Algorithm 2 up to n=50000 if n < 50001: t=Timer(lambda: maxsub.maxSubarray2(myArray,results)) alg2time = t.timeit(number=1) # timeit clocking function time for Algorithm 3 start = 0 end = len(myArray) -1 t = Timer(lambda: maxsub.maxSubarray3(myArray, start, end)) alg3time = t.timeit(number=1) # timeit clocking function time for Algorithm 4 t=Timer(lambda: maxsub.maxSubarray4(myArray,results)) alg4time = t.timeit(number=1) # Add times to the total alg1total = alg1total + alg1time alg2total = alg2total + alg2time
out_file.write(str(line[i]) + ', ') out_file.write(str(line[results[1]]) + ']') out_file.write('\n') out_file.write(str(results[2])) out_file.write('\n\n') # rewind to beginning of file and do it again with algorithm 2... out_file.write("Algorithm 2\n") in_file.seek(0) while (1): line = in_file.readline() if (not line) or (len(line) < 2): break line = [int(i) for i in line.replace("[","").replace(" ","").replace("]","").split(",")] print "Algorithm 2" t = Timer(lambda: maxsub.maxSubarray2(line, results)) print "Running time: " + str(t.timeit(number=1)) out_file.write('[') for i in range(results[0],results[1]): out_file.write(str(line[i]) + ', ') out_file.write(str(line[results[1]]) + ']') out_file.write('\n') out_file.write(str(results[2])) out_file.write('\n\n') # rewind to beginning of file and do it again with algorithm 3... out_file.write("Algorithm 3\n") in_file.seek(0) while (1): line = in_file.readline() if (not line) or (len(line) < 2):