Code Sample: Timing Functions for C++

This post is one of a series called “Code Examples” that gives snippets of code you can use in your own work. Other articles include: Processing AeroVis Outputs for Matlab

We often want to know the “timing” of codes — how long does it take to do each task?  This code sample uses the clock() function in C++, based on an example here.  Additionally, I also use classes here, since this is a nice way to get started doing some object oriented code.  You can copy the following into a new cpp file on the cluster and compile it using icpc timing.cpp -o timing, but you’ll eventually want to modify the classes and use the commands in your own projects.

Click to reveal the code, and I’ll provide some closing comments at the end.

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>

using namespace std;

class Timing
	clock_t startCalcA, endCalcA;
	clock_t startCalcB, endCalcB;
	void openTimingFile(string filename);
	void closeTimingFile();
	void write(double input);
	void write(string input);
	ofstream out;

void Timing::openTimingFile(string filename)
{, ios::out);
	if (!out) exit(-1); //exits if file didn't open
void Timing::closeTimingFile()
void Timing::write(string input)
	out << input;
void Timing::write(double input)
	out << " " << input;

Timing timing; //declare global instance of Timing class

int main()
	//open timing file:

	//place these statements around commands you want to time:
	timing.startCalcA = clock();
	for (int i = 0; i < 100000; i++) {cout << i << endl;}
	timing.endCalcA = clock();

	//write the output using the class functions:
	timing.write("task A");
	timing.write( ( double(timing.endCalcA)
		- double(timing.startCalcA) )
	timing.write("\n"); //start a new line

	//repeat for another task:
	timing.startCalcB = clock();
	int sum = 0;
	for (int i = 0; i < 100000; i++) {sum += i;}
	timing.endCalcB = clock();
	timing.write("task B");
	timing.write( ( double(timing.endCalcB)
		- double(timing.startCalcB) )

	//close timing file

	return 0;


A few notes:

  • This shows how time intensive it is to perform console or file i/o compared to performing calculations in memory.  Writing to a file took about 0.45 seconds in my example, whereas the calculations took no appreciable time.  So try to write to a file as infrequently as possible in your codes!
  • The class here makes the actual clock_t elements public. This means anyone can access them and modify them. This isn’t typically recommended when creating classes, but it makes it easy to time new elements. Instead of having to create get and set methods for each new task you want to time, you can just create new public clock_t elements, record the times, and output them.
  • Based on Matt Woodruff’s advice, I did encapsulate the output stream as a private variable, which allows us to create nice “write” functions that hide the details of how the timing is being written or where it’s going. In some applications, you may want to output timing information to the console instead of a file, and there you can simply change the “write” function to use cout instead of the file-based out stream.
  • The implementation of write could probably be improved. One issue is where to put the space and endline characters. Here I’m assuming that the user wants a space in front of every double, and the user has to manually output a newline character through a separate call to write. You could also insert a newline character directly in the definition of write, it’s up to you.
  • In my experience, the best way to start using classes is to start with small examples like this. The filenaming convention I use is to name a cpp file after the class (i.e., Timing.cpp) and allow each class to have its own header, Timing.h. In a future post, I’ll talk about how to link different files across a project instead of having them all in a single file such as this example

One thought on “Code Sample: Timing Functions for C++

  1. Pingback: Code Sample: Modify AeroVis files for Matlab « Pat Reed Group Research Tips Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s