ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello there, this is not an issue I have, I am more interesting in knowing what you think of the hability of C++ to do functional programming.
Here, I am using the STL as much as I can because it is so convenient, actually. I also (sometimes) like functional programming, like what the std::algorithm + functional can do: clearer code (not always, I know, but for simple things, definitely) and less error prone. However, using algorithms always perform slower than an explicit loop (item 43 of Meyers' Effective STL will explain you with lots of details how the std::algorithm performs faster...... profiling shows me explicit loops are always more efficient!); and I am not even 100% convinced that using iterators instead of std::vector.at(index) in a loop is actually faster.
Now, speed matters in what I am doing (scientific programming) as well as style (I have a student who is supposed to learn something) and obviously I want my calculations to be correct.
Clearer code, good style are easier to debug so now, I use std::algorithm when it actually makes the code simpler. But performance?
As a C++ developper, what do you think of std::algorithm and these issues?
I've written several intensive algorithms in C++, but I've never used std::algorithm. I can tell you from experience that checked iterators are detrimental to performance when not optimized. These iterators carry a pointer to their owning list so that operations on them verify that their pointer value correlates to the correct list. In reality, optimized iterators can somehow be faster than pointers.
Using template functions and classes don't reduce performance by virtue-of-being alone since they are merely for compile-time expansion. The drag comes from internal checks which allow a function to be generic enough to safely perform the given operation with minimal requirements of arguments and no assumptions made otherwise. I always find it easier and faster to control the context of algorithm function calls so that I can make certain assumptions, and can therefore optimize out certain things.
With C++, you have the convenience of creating a private, potentially unsafe, optimized function while having a public wrapper function which always ensures the function is called in a safe manner. If you know how to write a loop that's safe, I'd choose that over a generic algorithm any day. I'd make it a template, though, and follow the basic STL iterator principles to stay safe when processing an STL container. The detriment caused by using iterators can be more than made up for with any compiler worth anything (such as gcc.) Hope that helps.
PS For any code with looping which has to be fast, I find it best to default to what's essentially C code written in a C++ file to allow simple iterator incrementation.
Thank you very much for your answer, it is really helpful. Now I am just trying to "make it work" and mostly do profiling to check my algorithms. So I go for what I find safer. At a later stage, however, I will probably change it and follow your advices.