0

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?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Duki
0

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";
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.