I have two implementations of a function and would like to see which is faster. Call them foo1()
and foo2()
. I have a set of test cases I would like to run against both of them. The tests cases are stored in an array and I don't want to include the time spent accessing the array. Here is my code. It doesn't compile on duration += std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 )
with error message error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'std::chrono::microseconds' {aka 'std::chrono::duration<long long int, std::ratio<1, 1000000> >'})
52 | std::cout << "duration: " << duration << std::endl;
.
Code:
/*[first argument, second argument, correct result]*/
int inout[5][3] = {
{10, 4, 5},
{21, 4, 6},
{22, 4, 7},
{50, 5, 19},
{100, 5, 7},
//just example, real one is longer
};
std::chrono::microseconds duration = std::chrono::microseconds::zero();
for(auto& i : inout)
{
auto t1 = std::chrono::high_resolution_clock::now();
foo1(i[0], i[1]);
auto t2 = std::chrono::high_resolution_clock::now();
duration += std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();//this won't compile
}
std::cout << "duration of foo1(): " << duration << std::endl;
duration = std::chrono::microseconds::zero();
for(auto& i : inout)
{
auto t1 = std::chrono::high_resolution_clock::now();
foo2(i[0], i[1]);
auto t2 = std::chrono::high_resolution_clock::now();
duration += std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();//this won't compile
}
std::cout << "duration of foo2(): " << duration << std::endl;
Any suggestions on anything here? I was thinking it's more meaningful to test the speed for cache misses so should the calls to foo1()
and foo2()
be interlaced? OTOH the goal is to see which is faster and both would benefit from the cache, however the bigger one would likely have more cache misses. Also out of curiosity what is the type on auto& i
in the foreach loop?