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?

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.

Edited 6 Years Ago by UberJoker: n/a

Thanks Uberjoker, its works beautiful. Last question (which may be dumb) is what the unit of measure would be. Seconds? 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.

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

This article has been dead for over six months. Start a new discussion instead.