从任何内存地址读取 UInt32 最有效的方法是什么?
从 C++ 中的任意内存地址读取 UInt32 值的最有效方法是什么? (假设 Windows x86 或 Windows x64 体系结构。)
例如,考虑使用一个字节指针,该指针指向内存中某个位置的块,该块包含所有混合在一起的整数、字符串数据等的组合。以下示例显示了循环读取此块中的各个字段。
typedef unsigned char* BytePtr;
typedef unsigned int UInt32;
...
BytePtr pCurrent = ...;
while ( *pCurrent != 0 )
{
...
if ( *pCurrent == ... )
{
UInt32 nValue = *( (UInt32*) ( pCurrent + 1 ) ); // line A
...
}
pCurrent += ...;
}
如果在 A
行,pPtr
恰好包含 4 字节对齐的地址,则读取 UInt32 应该是一次内存读取。如果 pPtr 包含未对齐的地址,则可能需要多个内存周期,这会减慢代码速度。有没有更快的方法从非对齐地址读取值?
What would be the most efficient way to read a UInt32 value from an arbitrary memory address in C++? (Assuming Windows x86 or Windows x64 architecture.)
For example, consider having a byte pointer that points somewhere in memory to block that contains a combination of ints, string data, etc., all mixed together. The following sample shows reading the various fields from this block in a loop.
typedef unsigned char* BytePtr;
typedef unsigned int UInt32;
...
BytePtr pCurrent = ...;
while ( *pCurrent != 0 )
{
...
if ( *pCurrent == ... )
{
UInt32 nValue = *( (UInt32*) ( pCurrent + 1 ) ); // line A
...
}
pCurrent += ...;
}
If at line A
, pPtr
happens to contain a 4-byte-aligned address, reading the UInt32 should be a single memory read. If pPtr
contains a non-aligned address, more than one memory cycles my be needed which slows the code down. Is there a faster way to read the value from non-aligned addresses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议将 memcpy 放入循环中的 UInt32 类型的临时文件中。
这利用了这样一个事实:在启用优化的情况下构建时,编译器将内联四字节 memcpy,并且还有一些其他好处:
I'd recommend memcpy into a temporary of type UInt32 within your loop.
This takes advantage of the fact that a four byte memcpy will be inlined by the compiler when building with optimization enabled, and has a few other benefits:
您的代码是未定义的行为。
几乎唯一“正确”的解决方案是仅将某些内容读取为类型
T
(如果它是 类型T
),如下所示:例如,您想要读取一个整数,唯一的方法就是拥有一个整数。如果您希望它包含某种二进制表示形式,则需要将该数据复制到从变量开头开始的地址。
Your code is undefined behaviour.
Pretty much the only "correct" solution is to only read something as a type
T
if it is a typeT
, as follows:In this example, you want to read an integer, and the only way to do that is to have an integer. If you want it to contain a certain binary representation, you need to copy that data to the address starting at the beginning of the variable.
让编译器来做优化!
Let the compiler do the optimizing!