问题描述
我经常发现自己不得不重新present,由非常小的值的结构。例如,富
有4个值, A,B,C,D
的,范围从 0至3
。通常我不关心,但有些时候,这些结构
Often I find myself having to represent a structure that consists of very small values. For example, Foo
has 4 values, a, b, c, d
that, range from 0 to 3
. Usually I don't care, but sometimes, those structures are
-
在一个紧密的循环使用;
used in a tight loop;
它们的值读了十亿次/秒,那就是在程序的瓶颈;
their values are read a billion times/s, and that is the bottleneck of the program;
整个程序由数十亿的富
个大阵;
the whole program consists of a big array of billions of Foo
s;
在这种情况下,我觉得无法决定如何富
高效再present自己。我基本上有4个选项:
In that case, I find myself having trouble deciding how to represent Foo
efficiently. I have basically 4 options:
struct Foo {
int a;
int b;
int c;
int d;
};
struct Foo {
char a;
char b;
char c;
char d;
};
struct Foo {
char abcd;
};
struct FourFoos {
int abcd_abcd_abcd_abcd;
};
他们使用128,32,8,分别为每富
8位,从稀疏到密集。第一个例子可能是最语言之一,但使用它会通过方案,这听起来并不完全正确的尺寸为16倍实质上增加。此外,大部分内存将使用零填充,而不是在所有的被使用,这使我不知道这是不是一种浪费。另一方面手,包装他们密集带来了额外的开销为阅读。
They use 128, 32, 8, 8 bits respectively per Foo
, ranging from sparse to densely packed. The first example is probably the most linguistic one, but using it would essentially increase by 16 times the size of the program, which doesn't sound quite right. Moreover, most of the memory will be filled with zeroes and not be used at all, which makes me wonder if this isn't a waste. On the other hands, packing them densely brings an additional overhead for of reading them.
什么是一个结构重新presenting小值计算'最快'的方法?
What is the computationally 'fastest' method for representing small values in a struct?
推荐答案
有关密实,不产生大的开销阅读,我建议你与位域的结构体。当你有四个值的范围从0到3你的榜样,你定义结构如下:
For dense packing that doesn't incur a large overhead of reading, I'd recommend a struct with bitfields. In your example where you have four values ranging from 0 to 3, you'd define the struct as follows:
struct Foo {
unsigned char a:2;
unsigned char b:2;
unsigned char c:2;
unsigned char d:2;
}
这有一个大小为1字节,并且字段可以简单地访问,即 foo.a
, foo.b
等
This has a size of 1 byte, and the fields can be accessed simply, i.e. foo.a
, foo.b
, etc.
通过使你的结构更加密集,这应该帮助的缓存效率。
By making your struct more densely packed, that should help with cache efficiency.
编辑:
要总结注释:
有还是位摆弄与位域发生的事情,但它是由编译器完成,将最有可能比你手工写什么更有效的(更不用说它使你的源代码code更简洁,更不容易引入错误)。并给出了大量的你会处理结构中,通过使用包装的结构得到了高速缓存未命中像这样的减少可能会弥补位操作的结构强加的开销。
There's still bit fiddling happening with a bitfield, however it's done by the compiler and will most likely be more efficient than what you would write by hand (not to mention it makes your source code more concise and less prone to introducing bugs). And given the large amount of structs you'll be dealing with, the reduction of cache misses gained by using a packed struct such as this will likely make up for the overhead of bit manipulation the struct imposes.
这篇关于什么是在结构重新present小值的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!