Comparing programs

Apr 6, 2015 at 2:37pm
Hello. Is there any way I could compare my solution to the author's original solution? I mean which program ends faster in same inputs. Also, how can I use the tests for the program? There are .IN, .SOL and mere .txt files.
Apr 6, 2015 at 2:42pm
I didint know how to do this a few days ago, but Now Im taking a algorithm and datastructure class so I had to look up a way, it was pretty cool seeing differences.

If you're on windows.

#include <Windows.h>

Then, place this somewhere, like just globally above main or something

1
2
3
4
5
6
7
BOOL WINAPI QueryPerformanceCounter(
	_Out_  LARGE_INTEGER *lpPerformanceCount
	);

LARGE_INTEGER frequency;        // ticks per second
LARGE_INTEGER t1, t2;           // ticks
double elapsedTime;


Then have this at the top of main

QueryPerformanceFrequency(&frequency); // Gets ticks per second

This where you want to start taking time

QueryPerformanceCounter(&t1); // Starts Timer

And this where you want to stop the time

QueryPerformanceCounter(&t2); // Ends Timer

This is how you convert the time to seconds,milliseconds,microseconds (whatever you prefer, currently its milliseconds cuz * 1000)

elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000 / frequency.QuadPart; // Convert the time to milliseconds

And then you can just print out the time

1
2
std::cout << std::endl << "Time: " << elapsedTime << " Miliseconds.\n" << 
std::endl;


Hope this helped :)
Apr 6, 2015 at 3:22pm
Thanks. What if I have "cin>>" between start and end?
Apr 6, 2015 at 3:28pm
Nothing happens? You're supposed to put all your code between start and end (the code you wanted timed) for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{	QueryPerformanceFrequency(&frequency); // Gets ticks per second

	QueryPerformanceCounter(&t1); // Starts Timer

	std::string name;
	std::cout << "State your name: ";
	std::cin >> name;

	std::cout << name << " Yeh you're cool brah" << std::endl;

	QueryPerformanceCounter(&t2); // Ends Timer
	elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000 / frequency.QuadPart; // Convert the time to milliseconds
	std::cout << std::endl << "Time: " << elapsedTime << " Miliseconds.\n" << std::endl;
	system("pause");
	return 0;
}


For me, this ran in 1500 milliseconds. Obviously if you want a much more accurate time you'd have to run it many many times and then get the average.
Apr 6, 2015 at 3:31pm
Got it. Thank you.
But what about using tests?
Apr 6, 2015 at 3:39pm
What do you mean by that?
Apr 7, 2015 at 6:42pm
I meant:
There are .IN, .SOL and mere .txt files.

Do you know how to use them?
Topic archived. No new replies allowed.