Why a function returns an array is much slower than a subroutine returns an array? (real MWE included)

@kargl likely has much better advice on this matter. But I normally use -fmax-stack-var-size=10 in CMake files. Unless you are building a library to share with others, it makes more sense to allocate everything on the stack and increase the stack size for your application in a terminal before running the application rather than using heap arrays. There is a performance penalty with using heap arrays. Also, I think recursive functions run into trouble if heap arrays are used. In my cursory benchmarks, the performance penalty is around 5-10% compared to stack allocations. But I think that heavily depends on the usage and specific application. In Linux environment, try,

ulimit -s unlimited

to set the stack size to unlimited.

2 Likes