Boost::multi_array performance question(Boost::multi_array 性能问题)
问题描述
我正在尝试使用以下测试程序比较 boost::multi_array 与本机动态分配数组的性能:
I am trying to compare the performance of boost::multi_array to native dynamically allocated arrays, with the following test program:
#include <windows.h>
#define _SCL_SECURE_NO_WARNINGS
#define BOOST_DISABLE_ASSERTS
#include <boost/multi_array.hpp>
int main(int argc, char* argv[])
{
const int X_SIZE = 200;
const int Y_SIZE = 200;
const int ITERATIONS = 500;
unsigned int startTime = 0;
unsigned int endTime = 0;
// Create the boost array
typedef boost::multi_array<double, 2> ImageArrayType;
ImageArrayType boostMatrix(boost::extents[X_SIZE][Y_SIZE]);
// Create the native array
double *nativeMatrix = new double [X_SIZE * Y_SIZE];
//------------------Measure boost----------------------------------------------
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int y = 0; y < Y_SIZE; ++y)
{
for (int x = 0; x < X_SIZE; ++x)
{
boostMatrix[x][y] = 2.345;
}
}
}
endTime = ::GetTickCount();
printf("[Boost] Elapsed time: %6.3f seconds
", (endTime - startTime) / 1000.0);
//------------------Measure native-----------------------------------------------
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i)
{
for (int y = 0; y < Y_SIZE; ++y)
{
for (int x = 0; x < X_SIZE; ++x)
{
nativeMatrix[x + (y * X_SIZE)] = 2.345;
}
}
}
endTime = ::GetTickCount();
printf("[Native]Elapsed time: %6.3f seconds
", (endTime - startTime) / 1000.0);
return 0;
}
我得到以下结果:
[Boost] Elapsed time: 12.500 seconds
[Native]Elapsed time: 0.062 seconds
我不敢相信 multi_arrays 这么慢.谁能发现我做错了什么?
I can't believe multi_arrays are that much slower. Can anyone spot what I am doing wrong?
我认为缓存不是问题,因为我正在写入内存.
I assume caching is not an issue since I am doing writes to memory.
这是一个调试版本.根据 Laserallan 的建议,我做了一个发布版本:
This was a debug build. Per Laserallan's suggest I did a release build:
[Boost] Elapsed time: 0.266 seconds
[Native]Elapsed time: 0.016 seconds
更近了.但是 16 比 1 对我来说似乎仍然很高.
Much closer. But 16 to 1 still seems to high to me.
好吧,没有明确的答案,但我将继续前进,暂时将我的真实代码保留在本机数组中.
Well, no definitive answer, but I'm going to move on and leave my real code with native arrays for now.
接受 Laserallan 的回答,因为这是我测试中最大的缺陷.
Accepting Laserallan's answer because it was the biggest flaw in my test.
谢谢大家.
推荐答案
您是构建发布版还是调试版?
Are you building release or debug?
如果在调试模式下运行,boost 数组可能真的很慢,因为它们的模板魔法没有正确内联,在函数调用中产生大量开销.我不确定多数组是如何实现的,所以这可能完全关闭:)
If running in debug mode, the boost array might be really slow because their template magic isn't inlined properly giving lots of overhead in function calls. I'm not sure how multi array is implemented though so this might be totally off :)
也许存储顺序也存在一些差异,因此您可能将图像逐列存储并逐行写入.这会导致缓存行为不佳,并可能减慢速度.
Perhaps there is some difference in storage order as well so you might be having your image stored column by column and writing it row by row. This would give poor cache behavior and may slow down things.
尝试切换 X 和 Y 循环的顺序,看看是否有所收获.这里有一些关于存储排序的信息:http://www.boost.org/doc/libs/1_37_0/libs/multi_array/doc/user.html
Try switching the order of the X and Y loop and see if you gain anything. There is some info on the storage ordering here: http://www.boost.org/doc/libs/1_37_0/libs/multi_array/doc/user.html
由于您似乎正在使用二维数组进行图像处理,因此您可能有兴趣查看 boosts 图像处理库 gil.
Since you seem to be using the two dimensional array for image processing you might be interested in checking out boosts image processing library gil.
它可能具有开销较少的数组,非常适合您的情况.
It might have arrays with less overhead that works perfectly for your situation.
这篇关于Boost::multi_array 性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Boost::multi_array 性能问题
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01