You should be familiar with profilers just as you're familiar with debuggers and compilers. Depending on the development platform, you may either need a special compiler switch, or a special library, or nothing at all (AIX is cool like that), to create an executable that you can then run with the profiler and gather detailed statistics on execution time of every function.
But, to answer the question directly, at home, I use std::high_resolution_clock, as in
1 2 3 4 5 6
|
auto t1 = std::chrono::high_resolution_clock::now();
test_function();
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "test function took "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
<< " milliseconds\n";
|
At work, we have a stopwatch class in our corporate library that tracks user, cpu, and system time simultaneously.
In trivial cases like this, I find that it's simpler to compile the program to assembly language (option -S on gcc and many other compilers), and compare the produced machine code. For the array vs vector access loop, you'll find out that it is exactly the same - once you build a valid test case, of course. Your test loop does nothing and is removed completely by modern compilers)