加速循环中的函数调用

编程入门 行业动态 更新时间:2024-10-12 05:45:40
本文介绍了加速循环中的函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 loop over a very long container (millions elements) { each element compute 8 integers: k1,k2,k3,...,k8 call function func(k1,k2,k3,...,k8) } container is a std::vector, element is a long integer each k can only take {0,1,2,..5} six values. func is a simple expression of complex number calculation, involves std::conj and std::exp

为了加快速度,我将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.

更多推荐

加速循环中的函数调用

本文发布于:2023-06-05 11:08:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/518329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!