Hi all

I am new to C++ and have a few questions if that is ok.

I would like to get into good habbits early (i.e., using switch instead of 12 if else statments) and was wondering if anyone knew right off how to get a timer to time the compilation and execution of the program? I would like to test and tweak my work just to see what will make it faster and smoother. My prof said he knew there was one available but he couldn't think of it right off... any clue?

Also, I am looking for an extra credit problem. I was told that I might try writing programming "pearls" - techniques that elegantly solve common problems. If anyone has an idea of something creative please let me know. Note that I only have knowledge on iostream, fstream, iomanip, cmath, and control structures. Not sure how creative I can get with so little under my belt.

Thanks everyone!

#include <iostream>
#include <cstdio>
#include <ctime>

int main()
{
  std::clock_t start;
  double diff;

  start = std::clock();
  for ( int i = 0; i < 1000000; i++ )
    printf ( "iamthwee" );
  diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;

  std::cout<<"printf: "<< diff <<'\n';
}

>> was told that I might try writing programming "pearls

A common problem is to write a function that validates user integer input. It would get the value from the keyboard as a string then parse the string for illegal characters. If any illegal characters are found display an error message and ask for input again. Finally when no illegals are found convert the string to int and return the value.

#include <iostream>
#include <cstdio>
#include <ctime>

int main()
{
  std::clock_t start;
  double diff;

  start = std::clock();
  for ( int i = 0; i < 1000000; i++ )
    printf ( "iamthwee" );
  diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;

  std::cout<<"printf: "<< diff <<'\n';
}

How will I use this? part at the beginning and part at the end or what? :) thanks! :mrgreen:

Edited 3 Years Ago by pyTony: fixed formating

call clock() before starting the code you want to profile then again after the code finished. The difference is the approximate executation time. Since the code runs very quickly you probably have to run it quite a few times to get any measurable results. In his example he ran the print function 1,000,000 times. If you want the execution time per iteration then just make the simple division. (total time / number of iterations).

call clock() before starting the code you want to profile then again after the code finished. The difference is the approximate executation time. Since the code runs very quickly you probably have to run it quite a few times to get any measurable results. In his example he ran the print function 1,000,000 times. If you want the execution time per iteration then just make the simple division. (total time / number of iterations).

ah ok. i set it to fixed and showpoint, which gave more input for smaller apps.

could you explain the code to me? Sorry I'm asking you to tutor me, but I'm just interested in this.

mainly this part:
diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;


but i'm also lost on how the timer works... what does what in the code? it looks like a lot of uninitialized variables, but can't be because it works? :eek:

The clock function returns the number of clock ticks that have elapsed since the program started. One clock tick is typically 1 millionth of a second (called a millisecond) but it doesn't have to be. The macro CLOCKS_PER_SEC is the number of ticks per second. So if clock() returns a value of 2000000 then that is 2000000/CLOCK_PER_SEC or 2 seconds.

The line diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC; is just calculating the difference between the current time and the previous time, then converting it to seconds.

>>it looks like a lot of uninitialized variables
Look closely and you will see that his code initializes the variables correctly. Initializing variables to zero or some other value when declared is a good habit to get into, but its not absolutely required as long as the variables are initialized someplace before attempting to access them.

>How will I use this?
Wrap it in a class. It's much easier to use that way:

#include <ctime>

class jsw_timer {
public:
  typedef double diff_type;
public:
  jsw_timer(): start ( std::clock() ), elapsed ( 0.0 ) {}
public:
  void begin() { start = std::clock(); elapsed = 0.0; }
  diff_type end() {
    elapsed = static_cast<diff_type> ( std::clock() ) - start;
    return elapsed /= CLOCKS_PER_SEC;
  }
  diff_type last() const { return elapsed; }
private:
  std::clock_t start;
  diff_type    elapsed;
};
This article has been dead for over six months. Start a new discussion instead.