Hey guys,

we were given an opportunity for extra credit in my OOP class. We're going over recursion and my prof said if we could do a project using recursion and do another useing iterative statments, and time them, we would be given extra cred.

Is there a way to call the system clock at the begining of the loop and at the end; then subtract the 1st from the 2nd? Is there an easier way than this?

Recommended Answers

All 2 Replies

You can use the standard time library to do it, sort of:

#include <ctime>
#include <iostream>

void long_running_operation()
{
  for ( unsigned i = 1; i != 0; i++ )
    ;
}

int main()
{
  std::clock_t start;
  std::clock_t end;

  std::cout<<"Beginning timer..."<<std::endl;
  start = std::clock();

  long_running_operation();

  end = std::clock();
  std::cout<<"The operation took "
    << ( double ( end ) - start ) / CLOCKS_PER_SEC
    <<" seconds\n";
}

awesome thanks!

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.