Vincent Hui (6) [Avatar] Offline
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 We can see that function1 is much slower than function2.

How to achieve high performance Functional Programming in C++?
Ivan Cukic (97) [Avatar] Offline
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.
Vincent Hui (6) [Avatar] Offline
So far, I found the most simple way to avoid dynamic allocation is to use std::move.

My another question is
How to use std::move with monad to achieve high performance Functional Programming in C++?

or We can have other simple ways to achieve high performance Functional Programming in C++ without using std::move?

Thank a lot.

Efficient Pure Functional Programming in C++ Using Move Semantics