2

I want to know how can I calculate running time in O_notation in C++ programs? Is there any code for that?

I have to use this code for showing the running time

clock_t start, end;
start = clock();
//CODES GOES HERE

end = clock();

std::cout << end - start << "\n";
std::cout << (double) (end-start) / CLOCKS_PER_SEC;

But I want to calculated it in O_notation code to implement it in 2 programs min-heap and prim's algorithm with array.

betabandido
  • 18,946
  • 11
  • 62
  • 76
data
  • 39
  • 1
  • 1
  • 4
  • This is classic homework. Everything mentioned on your question has been implemented, measured, and explained before. Instead asking for code to do the work for you, learn about it. Start here: http://stackoverflow.com/questions/487258/plain-english-explanation-of-big-o – Jano Apr 26 '11 at 23:41
  • Measuring the big-oh completely defeats the point. You analyze the code to know it. *Then* you measure to check if you screwed it up. – Hans Passant Apr 27 '11 at 00:39

1 Answers1

2

Assuming you have well-defined formats for input and output, you stand a reasonable chance of running the code in question for various sizes of input, and doing (for example) a polynomial curve fit to the times take for the various sizes.

So, for example, you'd run the code for 10, 100, 1000, and 10000 inputs. If the run-time gets roughly 10 times as long with each change, you appear to have a linear algorithm. If it gets roughly 100 times as long each time, you appear to have a quadratic one, and so on.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111