#include <vector>
#include <iostream>

/* Link me with  -lrt  for clock_gettime(). */

inline double timediff(const struct timespec &a, const struct timespec &b)
{
	return a.tv_sec - b.tv_sec + .000000001 * (a.tv_nsec - b.tv_nsec);
}

std::vector<int> onethinginavector()
{
	static int a = 0;
	std::vector<int> v;
	v.push_back(a++);
	return v;
}

int onethingviavector()
{
	std::vector<int> v = onethinginavector();
	return v.front();
}

int onethingdirect()
{
	static int a = 0;
	return a++;
}

int main()
{
	const int repeatcount = 10000000;
	const double nanoseconds_per_second = 1e9;
	struct timespec starttime, stoptime;

	/* Measure direct time */
	int sum = 0;
	clock_gettime(CLOCK_REALTIME, &starttime);
	for (int repeat=0; repeat < repeatcount; ++repeat) {
		sum += onethingdirect();
	}
	clock_gettime(CLOCK_REALTIME, &stoptime);
	double directtime = timediff(stoptime, starttime);
	std::cout << "Direct: " << directtime << ", " << (nanoseconds_per_second * directtime / repeatcount) << " ns/call" << std::endl;

	/* Measure via vector time */
	clock_gettime(CLOCK_REALTIME, &starttime);
	for (int repeat=0; repeat < repeatcount; ++repeat) {
		sum += onethingviavector();
	}
	clock_gettime(CLOCK_REALTIME, &stoptime);
	double viavectortime = timediff(stoptime, starttime);
	std::cout << "Via vector: " << viavectortime << ", " << (nanoseconds_per_second * viavectortime / repeatcount) << " ns/call" << std::endl;

	std::cout << "Overhead: " << (nanoseconds_per_second * (viavectortime - directtime) / repeatcount) << " nanoseconds per call" << std::endl;
}

/* Typical output:

Direct: 0.0485153, 4.85153 ns/call
Via vector: 2.87153, 287.153 ns/call
Overhead: 282.302 nanoseconds per call

*/

