Will Caching affect time evaluation loops?

64 Views Asked by At

Consider a situation where I use the following code to evaluate time taken by a function to execute.

long double Measure_micro_sec ( void (*foo) () )
{
    auto start = std::chrono::system_clock::now();
    for(long i =0; i <1000000; ++i)
    {
        foo();
        foo();
        if(i%1000 == 0)
            std::cout << i << "iterations\n";
    }
    auto end = std::chrono::system_clock::now();
    std::chrono::duration<long double> t_1 = end - start;

    auto start = std::chrono::system_clock::now();
    for(long i =0; i <1000000; ++i)
    {
        foo();
        foo();
        foo();
        if( i%1000 == 0 )
            std::cout << i << "iterations\n";
    }
   auto end = std::chrono::system_clock::now();
    std::chrono::duration<long double> t_2 = end - start;

    return (t_2.count() - t_1.count());
}

In the above code the change in total time of execution of the loops differ by (theoretically) n times (1000000) the execution time of foo(). Therefore the execution time of foo() is (t2 - t1)/n seconds or (t2 - t1) micro seconds.

Will the function foo() be cached? Will it affect the computation of the execution time of foo()? i.e. will the time difference (t2-t1) be different for when foo() is cached and foo() is not cached.

Consider foo() to be :

void foo()
{
    std::cout << "Example function\n";
}

As @Hugo and @LightnessRacesinOrbit have said that caching is dependant on the function. Will this function be cached?

EDIT: I have used std::chrono::system_clock for use as timer

0

There are 0 best solutions below