3

Consider a slightly modified version of Fibonacci to test the performance between (lambda vs function) (with vs without) captures:

size_t fibFn(size_t n) {
  if (n <= 1) { return n; }
  return fibFn(n - 1) + fibFn(n - 2)+1+2;
//                                  ^~~~
//   This is modified so that I can | 
//        capture something outside |
//                    this function |
}

When I run this in Quickbench with Clang 10.0, I got a reasonable result:

fnNoCapture < lambdaNoCapture < fn << lambda

When I am just about to conclude that lambda with capture block is extremely slow, however, the result is almost completely inverted when I run this with GCC 10.1:

lambdaNoCapture > fnNoCapture >> fn > lambda

How is this possible? Is it because the two compilers implemented lambda in different ways?

EDIT: Even so, it makes no sense to me that lambda (with capture) can be so much faster than that without capture. The best case a compiler can optimize, in my pov, is to convert a lambda with capture to that without capture (e.g. by inlining variables), if possible.

jerryc05
  • 454
  • 1
  • 4
  • 16
  • 1
    Right off the bat, switching optimization to O2 tells a different story. – sweenish Dec 09 '20 at 20:39
  • @sweenish Sry I'm not sure what you meant by "tells a different story" could u elaborate? – jerryc05 Dec 09 '20 at 23:13
  • Are you sure the compiler cannot notice that you are running several times the same computation in a loop and keep just one iteration? – Marc Glisse Dec 10 '20 at 00:51
  • @MarcGlisse The compiler shall not optimize that. That is how Google Bench library shall work. – jerryc05 Dec 10 '20 at 01:31
  • The library cannot change what the language says. Maybe if you made N volatile, or called DoNotOptimize(N) in the loop before calling the function... The result remains surprising, but the ratio reduces. – Marc Glisse Dec 10 '20 at 07:22
  • See https://stackoverflow.com/q/32974625/1918193 and in particular -fno-optimize-sibling-calls . – Marc Glisse Dec 10 '20 at 07:45

0 Answers0