#Find average running time of ten input arrays of size n for i in range(10): # Zero out sums and averages alg1time = alg2time = alg3time = alg4time = 0 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
in_file = open('MSS_Problems.txt', 'r') out_file = open('MSS_Results.txt', 'w') #infinite looping ... out_file.write("Algorithm 1\n") while (1): # read a line from file line = in_file.readline() # if EOF or if line doesn't contain an array (second condition necessary on MSS_Problems.txt), break if (not line) or (len(line) < 2): break # line is assigned an array created from the integers in line, removing brackets, whitespace, and delimiting commas line = [int(i) for i in line.replace("[","").replace(" ","").replace("]","").split(",")] print "Algorithm 1" # timeit clocking function time t = Timer(lambda: maxsub.maxSubarray1(line, results)) # if we remove this print statement we will need to make sure we still run t.timeit for clocking and to get result print "Running time: " + str(t.timeit(number=1)) # write result as string to file 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 2... out_file.write("Algorithm 2\n") in_file.seek(0) while (1):