问题描述
如何在大数据块上强制使用内存分配?还是发生在您身上.
特别是在valarray上,我是否保证从0th索引获得最佳性能?
还可以检查最佳对齐方式吗?
例如,假设系统的内存按32位块对齐(这意味着,当CPU请求内存地址时,即使请求了一个字节,CPU也会获得整个32位块,而不仅仅是一个字节).
并且我定义了valarray[unsinged char] alignedArray
,那么通过alignmentArray [3]对alignmentArray [0]的访问不应对内存进行多次调用(应驻留在寄存器中).
总结一下,我有2个问题. 如何(以编程方式)检查内存对齐方式,并且如何确保0索引(或至少一个已知索引)位于单词的开头?
How do you force memory alginment on a large data chunk? Or does it happen for you.
Specifically on valarray am I guranteed optimal performance from the 0th index?
Also is there a way to check what the best alignment is?
For example, assumming the memory of the system is aligned in 32 bit chunks (meaning when the CPU requests a memory address it gets the whole 32 bit chunk not just a byte even if a byte was requested)
and I have a valarray[unsinged char] alignedArray
defined then the access to alignedArray[0] thru alignedArray[3] should not make more than one call into memory (should reside in the registers still).
To resummarize I have 2 questions. How do you check the memory alignment (programatically) and how do you ensure your 0 index (or atleast a known index) is at the start of the word?
推荐答案
bool TestWordBoundary(void* pTest)
{
INT_PTR pointer = (INT_PTR) pTest;
return (0 == pointer % sizeof(WORD));
}
void Yours()
{
const char* szBufferTest("content");
ASSERT(TestWordBoundary(&szBufferTest[0]));
ASSERT(!TestWordBoundary(&szBufferTest[1]));
}
这篇关于valarray上的内存对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!