「2次元配列へのラスタ捜査」の編集履歴(バックアップ)一覧はこちら
「2次元配列へのラスタ捜査」(2007/12/26 (水) 20:11:25) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
//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; // 行解放
}
}
実行結果 :
0.781
0.484
0
1
//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;
}
// 配列使用版
{
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;
}
// 配列 (動的確保) 使用版
{
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; // 行解放
}
}
実行結果 :
0.781
0.484
0
1