6 ms·
>Lambdas are also awesome when it comes to performance. Because they are objects rather than pointers they can be inlined very easily by the compiler, much like
by hellofunk 11y ago
>Lambdas are also awesome when it comes to performance. Because they are objects rather than pointers they can be inlined very easily by the compiler, much like functors. This means that calling a lambda many times (such as with std::sort or std::copy_if) is much better than using a global function. This is one example of where C++ is actually faster than C.
That's insight I've never seen anywhere before.
- stinos 11y agoI think there's truth in it but I'd still take it with a grain of salt, more like can be faster under the right circumstances. Inlining can not only be faster because there's no need for the function call prologue/epilogue etc but, probably more important, because the compiler sees all of the code and this opens doors for more optimizations. That's really important to realize; I recommend looking at the assembly generated by the compiler for loops etc from time to time. It's simply amazing what can be done. So if in similar code C++ can take advantage of it while C can't because it has to do an actual function call then the C++ version has advantages and might very well be faster (whereas otherwise both would produce roughly the same result and speed I guess, unless C++-only features like virtual functions etc used).
- exDM69 11y ago> So if in similar code C++ can take advantage of it while C can't because it has to do an actual function call then the C++ version has advantages and might very well be faster This is a matter of compilation units, not C vs. C++. If you put qsort() in a header file, and call it with a function pointer in the same compilation unit, it will get inlined and work exactly the same way as std::sort (which is in a header file since it's a template). You can pretty easily verify this yourself by doing a C++ template and a C function with function pointers (in a header file!), compile and inspect the resulting assembly code. Link-time optimization may relax the compilation unit requirement, but it's not very widely used yet.
- Kristine1975 11y agoIt's also wrong. If the compiler inlines e.g. std::sort, it doesn't matter whether the comparator is a lambda or a pointer to a function: The compiler knows what it is and can inline it as well. The only exception is when the function is defined in a different compilation unit. Then the compiler has no access to its definition and thus cannot inline it into std::sort. The "C++ with lambda is faster than C with function" comes from C's qsort function that takes a function pointer to a comparator and is not inlined by the compiler. This can result in it being slower than std::sort. BTW since lambdas without capture decay to function pointers, you can pass such a lambda to qsort.
- deleted 11y ago[deleted]
- gpderetta 11y agoIn principle there is no difference, in practice until relatively recently some popular compilers would fail to inline calls via function pointers. The reason is that the inliner would need to run after constant propagation and after the HOF function itself, i.e. sort, has been inlined. With function objects and templates you get a distinct instance of sort for each callback type so the target is known statically even before any optimisation starts.
- sebastic 11y agoHis logic is wrong. It's not because they are objects, it's because std::sort and friends are function templates. They know what they are calling at compile time. Templates allow for compile-time polymorphism like this. Because of compile-time polymorphism, you can pass in prvalue function pointers and objects alike and the compiler will be able to inline. C doesn't have templates, therefore it doesn't have compile-time polymorphism, therefore it's much harder for the compiler to inline. Compilers like GCC and Clang can inline in some runtime-polymorphism circumstances. See -flto and -fdevirtualize.
- exDM69 11y ago> C doesn't have templates, therefore it doesn't have compile-time polymorphism, therefore it's much harder for the compiler to inline. A C compiler can do the same inlining and optimizing as long as the called function (e.g. qsort) and the function that's pointed-to (e.g. the comparison function) are in the same compilation unit. C++ templates do not add any magic here, std::sort() is simply faster because it's in a header file and qsort() is not. If you pass a function pointer to a function in a different object file to std::sort, it won't have a lot of advantages over qsort in terms of optimizations/performance (it still does know sizeof(T), though). Link-time optimization should make standard qsort-type functions perform similarly to std::sort.
- sebastic 11y agoAlso C++ is generally as fast as C. If anything, it's faster thanks to increased opportunities for inlining. Whoever says C++ is slower than C is misinformed and/or overgeneralizing. Professional C++ programmers know this already though.
- raphaelj 11y agoDon't forget about the templates too. A generic collection is usually faster when implemented using templates instead of void*.
- jakub_h 11y agoUnless you use a DataDraw-like approach, I presume. I can't see templates doing similar amounts of data rearrangements.
- Kristine1975 11y agoYou'd be surprised: http://bannalia.blogspot.de/2015/09/c-encapsulation-for-data-oriented.html http://bannalia.blogspot.de/2015/09/c-encapsulation-for-data...
- jakub_h 11y agoSetting aside the cringe I experience whenever supposed principles of "OOP" are mentioned by the author, that page painfully reminds me of the that old joke about bolting four extra legs onto a dog... Furthermore, both a broader comparison (for more complex data and processing) and the technique I mentioned seems to be absent. However, I suppose it shows how to reclaim at least some benefits of better data arrangement even if the language isn't a good fit.
- Rexxar 11y agoDid you speak of this "datadraw.sourceforge.net" ? It's the first time I heard of it. Have you used it ?