对齐大小数组和非对齐大小数组的速度不同
我尝试对对齐大小数组和非对齐大小数组进行操作,但结果令我困惑,非对齐大小数组比对齐大小数组更快,这是我的代码:
TimeMeter timeMeter;
const int N = 100000;
_Tp A[64];
_Tp B[65];
int szA = sizeof(A);
int szB = sizeof(B);
// Method 1
timeMeter.start();
for ( int n = 0; n < N; n++ )
{
memset(A, 0, szA);
}
timeMeter.stop();
printf("Method 1 Time usage = %f ms\n", timeMeter.span());
// Method 2
timeMeter.start();
for ( int n = 0; n < N; n++ )
{
memset(B, 0, szB);
}
timeMeter.stop();
printf("Method 2 Time usage = %f ms\n", timeMeter.span());
- When
_Tp
为char
(8):方法 1 花费 2.195ms,方法 2 花费 2.175ms - 当
_Tp
为int
(32):方法1 花费 13.313ms,方法 2 花费 5.987ms - 当
_Tp
为double
(64) 时:方法 1 花费 14.266ms,方法 2 花费 11.304ms
I try to operate on aligned-size array and non-aligned-size array, but the result is a puzzle me, the non-aligned-size array is faster than aligned-size array, This is my code:
TimeMeter timeMeter;
const int N = 100000;
_Tp A[64];
_Tp B[65];
int szA = sizeof(A);
int szB = sizeof(B);
// Method 1
timeMeter.start();
for ( int n = 0; n < N; n++ )
{
memset(A, 0, szA);
}
timeMeter.stop();
printf("Method 1 Time usage = %f ms\n", timeMeter.span());
// Method 2
timeMeter.start();
for ( int n = 0; n < N; n++ )
{
memset(B, 0, szB);
}
timeMeter.stop();
printf("Method 2 Time usage = %f ms\n", timeMeter.span());
- When
_Tp
ischar
(8): Method 1 cost 2.195ms and Method 2 cost 2.175ms - When
_Tp
isint
(32): Method 1 cost 13.313ms and Method 2 cost 5.987ms - When
_Tp
isdouble
(64): Method 1 cost 14.266ms and Method 2 cost 11.304ms
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的基准测试无效有几个原因:
Your benchmark is invalid for several reasons:
类型只需在自身内部对齐,即
char
必须在 1 字节边界上对齐,int
必须在 4 字节边界上对齐,而double
必须在 8 字节边界上对齐。要真正测试未对齐的访问,请尝试执行
此外,memset 将所有内容视为指向一系列
char
的指针,这些字符永远不会未对齐,因此无论您对数组,您无法让memset
进行未对齐的写入。Types only have to be aligned within themselves, i.e.
char
must be aligned on a 1-byte boundary,int
must be aligned on a 4-byte boundary, anddouble
must be aligned on an 8-byte boundary.To really test unaligned accesses, try doing
Furthermore,
memset
treats everything like a pointer to a series ofchar
s, which are never unaligned, so no matter what you do with the array you can't getmemset
to do an unaligned write.