0

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

WARhead
  • 643
  • 5
  • 17
  • 1
    Why would branch prediction exist if it did not affect execution time? Whether `foo()` can be "cached" depends on what it is. We have no idea what your "arbitrary timer" does. Not enough detail here to answer. What did your results say? – Lightness Races in Orbit Mar 17 '18 at 15:13
  • @LightnessRacesinOrbit I have updated question to include a timer. And I wanted to find out if the answer i get is the average execution time when it is cached or when it is not. – WARhead Mar 17 '18 at 15:19
  • Look at the assembly. – Lightness Races in Orbit Mar 17 '18 at 16:17
  • I guess it depends on whether foo can be cached. What does it do? If it can be cached than optimization settings of the compiler could influence your code. Maybe you can make the example more like a real-life situation by mimicking random input data or something. – Hugo Mar 17 '18 at 16:57
  • @Hugo What I want is a generic function for evaluation of execution time. Therefore I want to know whether the result i get from this function as average execution time is when the function is cached or not. – WARhead Mar 18 '18 at 06:11
  • @Hugo What kind of functions are cached?? – WARhead Mar 18 '18 at 06:11
  • I played around with the [Compiler Explorer](https://godbolt.org/). If I make a function that multiplies a float with an int but does not return it and loop to call this function 45 times, the first optimization (-O) of g++ already removes the function. You could do a similar test with your functions. – Hugo Mar 18 '18 at 13:57
  • @Hugo so does that mean that i cannot make a generic time evaluation function? – WARhead Mar 18 '18 at 16:31
  • @LightnessRacesinOrbit I have only begun to view assembly code and do not know how to check if a function is cached or not – WARhead Mar 22 '18 at 17:34
  • I'm not really sure what you mean by "caching" a function. Do you mean eliding calls at compile-time when the function has no side effects and can be shown to have the same return value that it did before? Good luck finding a compiler that can do that through a function pointer :) – Lightness Races in Orbit Mar 22 '18 at 18:16
  • @LightnessRacesinOrbit So it won't happen to a function pointer? In essence what i want to know is if this function `Measure_micro_seconds `will be able to find the execution time of `foo` – WARhead Mar 23 '18 at 13:32

0 Answers0