Vincent Hui (5) [Avatar] Offline
#1
We know that dynamic memory allocation is time consuming. Assume we know how much memory we need for an output of a function, how to avoid multiple dynamic memory allocations in functional style?
I have an example http://quick-bench.com/QhDHBqlVkk_1FmXP-4wWIF9pHCw. We can see that function1 is much slower than function2.

How to achieve high performance Functional Programming in C++?
Ivan Cukic (94) [Avatar] Offline
#2
To be honest, I haven't spent much time with FP-specific optimizations. My rule-of-thumb would be to develop what you can in the FP-style, and use the usual C++ optimizations if you need them (when you detect a bottleneck) even if they don't fit FP best practices.

It is the same with OO practices. Just look at the std::getline - it is not a nice OO design, but its API is designed with efficiency in mind.

In your case, the same trick as std::getline would be applicable - to have the same vector instance reused over and over again, where it would need to be reallocated only of the size of the new vector is larger than the capacity of the old one.