为了加快速度,我将func的所有可能结果缓存到数组,并调用func_array [k1] [k2] [k3] ...。 但是如果简单地定义func_array为: std :: complex func_array [6] [6] [6] ...,程序在堆栈溢出时结束。
To speed up, I cache all possible outcome of 'func' to an array, and call func_array[k1][k2][k3]... instead. But if simply define func_array as: std::complex func_array[6][6][6]..., the program dies on stack overflow.
任何更好的解决方案以加快速度?
Any better solutions to speed up?
推荐答案c $ c> std :: complex [6] [6] ... [6] 首先,这很容易导致堆栈溢出:这是一个相当大的数组对于许多堆栈。第二:如果你在栈上创建它,每次你调用函数时,它将被重新初始化。你可能想要的是一个局部静态数组,将被初始化一次(遇到第一次),然后在函数调用之间保留它的值。
You're creating an array of std::complex[6][6]...[6] on the stack? First, this could easily cause stack overflow: that's a fairly large array for many stacks. And second: if you create it on the stack, it will be reinitialized each time you call the function. What you probably want is a local static array, which will be initialized once (the first time it is encountered), and then retain its value between function calls.
更多推荐
加速循环中的函数调用
发布评论