在 C++ 中分配大本地内存的最佳方法功能
我有一个程序,其中有一个相当大的内存块的表,它是一个包含两个整数的结构(每个整数必须至少有 4 个字节才能容纳 1,000,000,000)。该结构目前有 500 多个条目。所以我们谈论的是 4k。 4k 的堆栈空间看起来并不多,但对于一个千载难逢的函数来说确实有点浪费。 (假设我们将在程序的生命周期内调用它一次,因此如果分配速度稍慢,也没关系,因为该空间在分配时间内更有价值)。
该代码只是简单的值查找,生成它的代码相当繁重,而且我们只是在查找特定值。我已经有了代码,而且查找速度要快得多,所以毫无疑问我想走这条路。我可以添加文件加载以从文件中获取数据,但对我来说,至少在这一点上这似乎有点矫枉过正。
现在我可以想到两种分配方式。使其成为全局变量,并使其成为局部变量。显然,本地是可行的方法,因为只有这个函数需要该表。然而,我正在寻找的是,是否有任何我可以使用的关键字,或者加载该变量的任何方式(目前它只是这些对的数组),这将是最有益的。
我最好的情况是让它仅在函数范围内尝试进入内存,然后允许该内存返回到系统。以尽可能有效的方式。除了将数据本地化之外,我还应该采取其他步骤吗?
I have a program where I'm a table of a decently large block of memory, it's a structure with two integers (they would have to be at least 4 bytes a piece to hold up to 1,000,000,000). The structure currently has a little more than 500 entries into it. So we're talking about 4k. 4k of stack space doesn't seem like a lot, but it does seem wasteful for a function that may only be called once in a blue moon. (Assume we'll call it once over the life time of a program, so if it's slightly slow to allocate, it's ok as that space is more value over the allocation time).
The code is just a simple look up of values, the code to generate it is quite heavy, and we only are looking for specific values anyways. I've the code already, and the look up is by far faster, so there's no question that I want to go down this route. I could add in a file load to get the data out of a file, but to me that seems overkill at least at this point.
Now there's two ways I can think of allocating it. Making it a global variable, and making it a local variable. Obviously, local is the way to go as it's only this function that needs the table. However what I'm looking for is if there's any keywords I can use, or any way of loading that variable (currently it's just a array of these pairs) that will be the most beneficial.
My best case is to make it attempt to be in memory only for the scope of the function, and then to allow that memory to be returned to the system. in as efficient a way as possible. Is there a step beyond making the data local that I should take?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而已。它应该看起来像这样:
对于您的需求来说,它是否“缓慢”值得怀疑。如果您想改进这一点,指定对齐方式可能会有所帮助。
Nothing more. It should look like this:
It's doubtful that it is 'slow' for your needs. If you want to improve this, specifying the alignment could help.
只要在堆栈上分配
它就不会浪费,因为它只会在函数执行期间在堆栈上分配。
或者(如果堆栈空间不够),
这会将大部分分配的内存放在堆上
Just allocate it on the stack
That is not wasteful, as it will only be allocated on the stack during the execution of the function.
Alternatively (if stack space isn't plentiful),
which will put the bulk of allocated memory on the heap
如果它在函数调用之间是恒定的,并且您可以腾出 4K 的内存(您可能并不经常需要),则可以将其设为局部静态变量。
它的作用几乎与全局变量完全相同,只是它只能从该函数内部可见。
当您加载程序时,它所需的内存将在数据段中分配(与分配全局变量的位置相同),并在应用程序关闭时释放。根据生成此表的方式,这些值也可能在加载程序时设置,但前提是它们是常量。如果这些值在编译时未知,则将在第一次调用此函数时设置它们。
If it is constant between function calls and you could spare 4K of memory that you might not need that often, you could make it a local static variable.
It will act almost exactly as a global variable, except it will only be visible from inside this function.
The memory it needs will be allocated in the data segment (same place where globals are allocated) when you load your program and freed when the application closes. Depending on how you generate this table, the values might also be set when you load the program, but only if they are constant. If the values are not known at compile time, they will be set upon the first call of this function.