「2次元配列へのラスタ捜査」の編集履歴(バックアップ)一覧に戻る

2次元配列へのラスタ捜査 - (2007/06/08 (金) 16:35:27) のソース

//cpp/linenumber

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/timer.hpp>
#include <boost/numeric/ublas/matrix.hpp>

int main()
{
    namespace ublas = boost::numeric::ublas;
    const int M = 10000, N = 20000;

    // boost::ublas使用版
    {
        ublas::matrix<int> a( M, N );

        boost::timer t;
        std::fill( a.data().begin(), a.data().end(), int( 0 ) );
        std::cout << t.elapsed() << std::endl;
    }

    // std::vector試用版
    {
        using std::vector;
        vector<vector<int> > a( M, N );

        boost::timer t;
        for( vector<vector<int> >::iterator i=a.begin(); i != a.end(); ++i ) {
            fill( i->begin(), i->end(), 0 );
        }
        std::cout << t.elapsed() << std::endl;
    }

    // C配列試用版
    {
        int a[M][N];

        boost::timer t;
        for( int i=0; i < M; ++i ) {
            for( int j=0; j < N; ++j ) a[i][j] = 0;
        }
        std::cout << t.elapsed() << std::endl;
    }

    // C配列 (動的確保) 試用版
    {
        int **a = new int *[M];     // 行作成
        for( int i=0; i < M; ++i )  // 列作成
            a[i] = new int[N];

        boost::timer t;
        for( int i=0; i < M; ++i ) {
            for( int j=0; j < N; ++j ) a[i][j] = 0;
        }
        std::cout << t.elapsed() << std::endl;

        for( int i=0; i < M; ++i )
            delete [] a[i]; // 列解放
        delete [] a;        // 行解放
    }
}