的boost :: multi_array的性能问题 [英] Boost::multi_array performance question

查看:351
本文介绍了的boost :: multi_array的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提升比较的性能:: 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\n", (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\n", (endTime - startTime) / 1000.0);

    return 0;
}

我得到如下结果:

I get the following results:

[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.

那么,没有明确的答案,但我会继续前进,并留下我的真实code与本地阵列现在。

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.

感谢所有。

推荐答案

您构建释放或调试?

如果在调试模式下运行,升压阵列可能会因为他们的模板魔术不正确内联给予大量开销的函数调用会很慢。我不知道怎么阵列多的实施,虽然所以这可能是完全关闭:)

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循环的顺序,看看你得到什么。
有在这里的存储顺序的一些信息:
<一href=\"http://www.boost.org/doc/libs/1_37_0/libs/multi_array/doc/user.html\">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

编辑:
既然你似乎可以用图像处理二维数组,你可能会感兴趣的检查了提升图像处理库的gil

它可能有阵与完全适用于您的情况更少的开销。

It might have arrays with less overhead that works perfectly for your situation.

这篇关于的boost :: multi_array的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆