Exemplo n.º 1
0
def test_performance(graph, n=10):
	results = initialize_results()

	# Using 'a' flag
	attrib = 0
	start_time = time.time()
	for i in range(n):
		attrib += color_graph(graph, 'a')[1][0]
	results['a'].append((time.time() - start_time) / n)
	results['a'].append(attrib / n)

	# Using 'b' flag
	attrib = 0
	start_time = time.time()
	for i in range(n):
		attrib += color_graph(graph, 'b')[1][0]
	results['b'].append((time.time() - start_time) / n)
	results['b'].append(attrib / n)

	# Using 'c' flag
	attrib = 0
	start_time = time.time()
	for i in range(n):
		attrib += color_graph(graph, 'c')[1][0]
	results['c'].append((time.time() - start_time) / n)
	results['c'].append(attrib / n)

	# Using 'd' flag
	attrib = 0
	start_time = time.time()
	for i in range(n):
		attrib += color_graph(graph, 'd')[1][0]
	results['d'].append((time.time() - start_time) / n)
	results['d'].append(attrib / n)

	return results
Exemplo n.º 2
0
def main(argv=sys.argv):
	# In case of wrong list of arguments.
	if (len(argv) < 3 or len(argv) > 4):
		print('usage: %s input_file output_file [performance_file]' % argv[0])
		print('If performance_file is not specified, performance tests will not be run.')
		return False

	try:
		f = open(argv[1], 'r')  # Try to open the input file
	except OSError as e:
		print(e)
		return False
	else:
		# If successful, generate the graph from the file.
		graph, flag = parse_input(f)
		f.close()

	try:
		f = open(argv[2], 'w')  # Try to open the output file
	except OSError as e:
		print(e)
		return False
	else:
		# If successful, print the resulting colored graph.
		print_result(color_graph(graph, flag)[0], f)
		f.close()

	# If there is one performance file specified,
	# run the performance tests.
	if len(argv) == 4:
		results = test_performance(graph, 50)

		try:
			f = open(argv[3], 'w')
		except OSError as e:
			print(e)
			return False
		else:
			for flag in results:
				print("Results for '%s' flag:" % flag, file=f)
				print("\tAverage time taken to color graph: %.5fs"
					% results.get(flag)[0], file=f)
				print("\tAverage number of attributions: %.0f\n" 
					% results.get(flag)[1], file=f)
			f.close()

	return True