Hello,

I have implemented different sorting algorithms (bubble sort, merge sort, heap sort, etc...) and want to calculate the running time for each one on different sets of data. I'm using Visual Studiois but am familiar with Dev C++ and Xcode on Mac. I used C++. Is there some special command or menu option I can click so that I can record the running time and compare results?

All 13 Replies

In the <ctime> header there is a function called clock() and it tells you in miliseconds the amount of time passed since the start of the program. If you are testing one algorithm this will mostlikly give a value of 1 or 0. A way around this is to run the algorithm many times (1000 or something) and then just divide the time passed by times ran to get the average.

I use timestamp outputs to the screen or a debug file for this sort of thing.

clock_t startClock,finishClock;
double timeCount;
startClock = clock();
//-----your sort function goes here here--------
finishClock = clock();
timeCount = finishClock - startClock ;

timeCount will give you ELAPSED CLOCK. If you divide that by 100 ie: timeCount/100...you will get ELAPSED TIME.

Also, dont forget to import the following.
#include <time.h>
#include <ctime>

hope that helps. Good luck.

i'll add the code and come back with my results.

Thanks Uberjoker, its works beautiful. Last question (which may be dumb) is what the unit of measure would be. Seconds? Milliseconds?

milliseconds

Well, i'd like to ask you that how can we measure the running times of algorithms with counting basic operations, what i'm asking is how can i put a counter into my program and to learn the numbers of basic operations are executed.

Well, i'd like to ask you that how can we measure the running times of algorithms with counting basic operations, what i'm asking is how can i put a counter into my program to learn the numbers of basic operations are executed.

you just want to know how many operations the sort function does?.

yes, but what i want to know is how to measure the basic operations(which are executed most in the sort function.) actually, i want to experience the complexity of a sort function with counting its operations.

you mean you want to actually know whats going on during the sorting?...for that read the theory behind it. i am not sure sure how you can actually "experience" the complexity of a sorting function unless you do it in your head :)

yes i mean i'd like to find out what's going on during sorting.
When i'm saying experience complexity, for example, theoretically it's known that a selection sort algorithm has a time complexity of O(n^2), what i want is with changing input sizes of an array and record the execution times for each input size.I mean, instead of running time, doing it with a counter. i hope i could explain what i want to ask

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.