如何将堆栈性能提升上分配数组? [英] How to allocate arrays on the stack for performance gains?

查看:110
本文介绍了如何将堆栈性能提升上分配数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些功能,如 popcount 最优化的版本和计数连续零使用查表来得到最终的的回答。

在C和C ++可以分配在堆栈上阵列和快速访问它们。

有没有办法做到这一点在C#中呢?据我所知, stackalloc 只能函数中使用,因此该数组将不能持续。

我有我希望能够尽可能快地访问,因此将preFER小查找表来为它分配堆栈,而不是堆的。


解决方案

  

我有我希望能够尽可能快地访问,因此将preFER小查找表来为它分配堆栈,而不是堆的。


这说法是混淆。把东西在堆栈上意味着它每次你进入它的声明的作用时间重新初始化。通常的优化是这样的数据,来代替存储在永久位置,如一个静态变量。

例如,这里有一个样本 popcount()实施从的汉明重量维基百科的文章:

 静态uint8_t有wordbits [65536] = {/ *到65535整数0 bitcounts,包括* /};
静态INT popcount(uint32_t的我)
{
    返回(wordbits [I和为0xFFFF] + wordbits [I>> 16]);
}

请注意, wordbits 数组声明的之外的任何功能的,作为一个静态变量。

在C#中类似的声明是这样的:

 静态只读的byte [] wordbits = {/ *到65535整数0 bitcounts,包括* /};
静态INT popcount(UINT I)
{
    返回(wordbits [I和为0xFFFF] + wordbits [I>> 16]);
}

请注意使用C#的只读关键字明确指出,该对象仅会进行一次初始化。

(很明显,在这两个例子中阵列中的注释由实际值替换。替换地,它们可以在运行时计算一次,并保存到数组)。

从你的问题,好像也许你至少有点困惑堆与堆与数据段(即一个特殊的范围内存在的可执行映像直读入内存)。出于性能,堆栈分配是如果你正在处理中经常分配和固定大小的物体,你不想遭受通过内存管理器分配的成本是有用的。

但在堆栈上分配不提供任何性能优势而言实际的访问的数据,肯定也不会方面提供任何的性能优势的初始化的数据。事实上,对后者的数量将花费你更多,因为你必须每次进入函数的时间进行初始化。

我认为上面应该充分解决您的问题。但如果不是,请查看它是什么,你实际上是试图做的,和编辑你的问题,使其更清晰。您可以检查我怎么问一个很好的问题讨教如何更好present你的问题了清晰,回答的方式。

Some of the most optimal versions of functions like popcount and count consecutive zeros use table lookups to get the final answer.

In C and C++ one can allocate arrays on the stack and access them quickly.

Is there a way to do this in C# as well? As far as I know, stackalloc can only be used within functions and thus the array won't persist.

I have a small lookup table that I'd like to be able to access as quickly as possible and thus would prefer to allocate it on the stack rather than heap.

解决方案

I have a small lookup table that I'd like to be able to access as quickly as possible and thus would prefer to allocate it on the stack rather than heap.

That statement is confusing. Putting something on the stack means it has to be reinitialized every time you enter the function in which it's declared. The usual "optimization" is to instead store such data in a persistent location, such as a static variable.

For example, here's a sample popcount() implementation from the Hamming weight Wikipedia article:

static uint8_t wordbits[65536] = { /* bitcounts of integers 0 through 65535, inclusive */ };
static int popcount(uint32_t i)
{
    return (wordbits[i&0xFFFF] + wordbits[i>>16]);
}

Note that the wordbits array is declared outside of any function, as a static variable.

A similar declaration in C# would be something like this:

static readonly byte[] wordbits = { /* bitcounts of integers 0 through 65535, inclusive */  };
static int popcount(uint i)
{
    return (wordbits[i & 0xFFFF] + wordbits[i >> 16]);
}

Note the use of C#'s readonly keyword to make clear that this object will only ever be initialized once.

(Obviously, in both examples the comment in the array is replaced by actual values. Alternatively, they can be computed once at runtime and saved into the array).

From your question, it seems like maybe you're at least a little confused about stack vs. heap vs. data segment (i.e. a special range of memory read straight from an executable image into memory). For performance, stack allocations are useful if you're dealing with fixed-sized objects that are allocated frequently and which you don't want to suffer the cost of allocating via the memory manager.

But allocating on the stack doesn't offer any performance benefit in terms of actually accessing the data, and definitely also does not offer any performance benefit in terms of initializing the data. Indeed, on the latter count it would cost you more because you'd have to initialize it each time you enter the function.

I believe that the above ought to adequately address your concern. But if not, please review what it is you're actually trying to do, and edit your question so that it is more clear. You can check How do I ask a good question for advice on how to better present your question in a clear, answerable way.

这篇关于如何将堆栈性能提升上分配数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆