C ++很慢? [英] C++ is Slow?

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

问题描述

大家好,


我经常面对以下两种技巧,我相信它们通常会产生不太可读的代码,但我是告诉你更快

因此更好。任何人都可以帮我解决反例和

参数吗?


1. STL很慢。


更具体的是矢量。这个论点是这样的:


多维数组应该被分配为大的连续

块。这样,当您访问数组并到达行的末尾时,下一行将已经在缓存中。你也可以在访问数组时花费时间导航指针。

所以应该像这样创建一个大小为100x100的二维数组:


const int xdim = 100;

const int ydim = 100;


int * myarray = malloc(xdim * ydim * sizeof(int));


并按如下方式访问:


myarray [xdim * ypos + xpos] = avalue;


这个论点合理吗? (对我来说听起来很合理,虽然我已经完成的小额测试通常不会显示任何重大的

差异)。


对我来说这个语法看起来很可怕,我错了吗?向量是否错误

容器使用? (我通常的解决方案是向量< vector< int)。

使用valarray帮助吗?


2. iostream很慢。


我最近遇到过这个问题。我之前没有考虑过,

我喜欢语法而且一般都不做那么多IO ...我只是现在

开始处理数TB的数据,因此它将成为一个问题。

iostream慢吗?特别是我遇到了以下例子

谷歌搜索。 stdio版本运行大约1秒,

iostream版本需要8秒。这只是一个糟糕的iostream

实现? (OS X上的gcc 4)。或者有没有理由说明为什么iostream对某些操作来说基本上更慢?
?有什么东西我应该记住加速io吗?


// stdio版本

#include< cstdio>

使用命名空间std;

const int NULA = 0;

int main(void){

for(int i = 0; i< 100000000; ++ i)

printf(" a");

返回NULA;

}


// cout版本

#include< iostream>

使用命名空间std;

const int NULA = 0;

int main(void){

std :: ios_base :: sync_with_stdio(false);


for(int i = 0; i< 100000000; ++ i)

cout<< " A" ;

返回NULA;

}

解决方案

nw写道:
< blockquote class =post_quotes>
大家好,


我经常面对以下两种技巧,我经常相信
产生不太可读的代码,但我被告知更快

因此更好。任何人都可以帮我解决反例和

参数吗?


1. STL很慢。


更具体的是矢量。这个论点是这样的:


多维数组应该被分配为大的连续

块。这样,当您访问数组并到达行的末尾时,下一行将已经在缓存中。你也可以在访问数组时花费时间导航指针。

所以应该像这样创建一个大小为100x100的二维数组:


const int xdim = 100;

const int ydim = 100;


int * myarray = malloc(xdim * ydim * sizeof(int));


并按如下方式访问:


myarray [xdim * ypos + xpos] = avalue;


这个论点合理吗? (对我来说听起来很合理,虽然我已经完成的小额测试通常不会显示任何重大的

差异)。


对我来说这个语法看起来很可怕,我错了吗?向量是否错误

容器使用? (我通常的解决方案是矢量< vector< int)。

使用valarray帮助?



在正常情况下,你应该使用你提到的矢量。


2. iostream很慢。


我最近遇到过这个问题。我之前没有考虑过,

我喜欢语法而且一般都不做那么多IO ...我只是现在

开始处理数TB的数据,因此它将成为一个问题。

iostream慢吗?特别是我遇到了以下例子

谷歌搜索。 stdio版本运行大约1秒,

iostream版本需要8秒。这只是一个糟糕的iostream

实现? (OS X上的gcc 4)。或者有没有理由说明为什么iostream对某些操作来说基本上更慢?
?有什么东西我应该记住加速io吗?


// stdio版本

#include< cstdio>

使用命名空间std;

const int NULA = 0;

int main(void){

for(int i = 0; i< 100000000; ++ i)

printf(" a");

返回NULA;

}


// cout版本

#include< iostream>

使用命名空间std;

const int NULA = 0;

int main(void){

std :: ios_base :: sync_with_stdio(false);


for(int i = 0; i< 100000000; ++ i)

cout<< " A" ;

返回NULA;

}



我认为这是一个实施问题。这两个代码应该以相同的时间成本承担

。如果您删除

" std :: ios_base :: sync_with_stdio(false);"输出较慢?

另外检查编译器优化开关,它们可以增强运行时执行的运行时间。


< blockquote> nw写道:


>

更具体地说是矢量。这个论点是这样的:>

"多维数组应该被分配为大的连续的

块。这样,当您访问数组并到达行的末尾时,下一行将已经在缓存中。你也可以在访问数组时花费时间导航指针。

所以应该像这样创建一个大小为100x100的二维数组:


const int xdim = 100;

const int ydim = 100;


int * myarray = malloc(xdim * ydim * sizeof(int));


并按如下方式访问:


myarray [xdim * ypos + xpos] = avalue;


这个论点合理吗? (对我来说听起来很合理,虽然我已经完成的小额测试通常不会显示任何重大的

差异)。


对我来说这个语法看起来很可怕,我错了吗?向量是否错误

容器使用? (我通常的解决方案是矢量< vector< int)。

使用valarray帮助?



我认为您的代码不正确。您的方法已更正:

#include< cstdlib>


int main()

{

使用命名空间std;


const int XDIM = 100;


const int YDIM = 200;

int (* my_array)[YDIM] = static_cast< int(*)[200](malloc(XDIM * YDIM

* ** my_array));


if(my_array == 0)

返回EXIT_FAILURE;


for(size_t i = 0; i< XDIM; ++ i)

for(size_t j = 0; j< YDIM; ++ j)

my_array [i] [j] = i + j;

// .. 。


免费(my_array);


// ...

}

等效的C ++风格:

包含< cstdlib>


int main()

{

using namespace std;


const int XDIM = 100;


const int YDIM = 200;

int(* my_array)[YDIM] = new int [XDIM] [YDIM];


for(size_t i = 0; i< XDIM; ++ i)

for(size_t j = 0; j< YDIM; ++ j)

my_ar ray [i] [j] = i + j;

}


正确的C ++方法:


#include< cstdlib>

#include< vector>


int main()

{

using namespace std;


const int XDIM = 100;


const int YDIM = 200;

vector< vector< int my_array(XDIM,vector< int>(YDIM));


for(vector< vector< int :: size_type i = 0; I< my_array.size(); ++ i)

for(vector< int> :: size_type j = 0; j< my_array [i] .size(); ++ j)

my_array [ i] [j] = i + j;

// ...


//无需清理内存或任何其他资源/>
// - RAII(资源获取初始化)


}


Ioannis Vranos< iv * ****@nospam.no.spamfreemail.grwrote:


正确的C ++方法:


vector< vector< int my_array(XDIM,vector< int>(YDIM));



这不一定是正确的方法。上面创建了YDIM + 1

单独的代码块,可能是,也可能不是一个好主意。


Hi all,

I''m constantly confronted with the following two techniques, which I
believe often produce less readable code, but I am told are faster
therefore better. Can anyone help me out with counter examples and
arguments?

1. The STL is slow.

More specifically vector. The argument goes like this:

"Multidimensional arrays should be allocated as large contiguous
blocks. This is so that when you are accessing the array and reach the
end of a row, the next row will already be in the cache. You also
don''t need to spend time navigating pointers when accessing the array.
So a 2 dimensional array of size 100x100 should be created like this:

const int xdim=100;
const int ydim=100;

int *myarray = malloc(xdim*ydim*sizeof(int));

and accessed like this:

myarray[xdim*ypos+xpos] = avalue;

Is this argument reasonable? (Sounds reasonable to me, though the
small tests I''ve performed don''t usually show any significant
difference).

To me this syntax looks horrible, am I wrong? Is vector the wrong
container to use? (My usual solution would be a vector<vector<int).
Would using a valarray help?

2. iostream is slow.

I''ve encountered this is work recently. I''d not considered it before,
I like the syntax and don''t do so much IO generally... I''m just now
starting to process terabytes of data, so it''ll become an issue. Is
iostream slow? specifically I encountered the following example
googling around. The stdio version runs in around 1second, the
iostream version takes 8seconds. Is this just down to a poor iostream
implementation? (gcc 4 on OS X). Or are there reasons why iostream is
fundamentally slower for certain operations? Are there things I should
be keeping in mind to speed up io?

// stdio version
#include <cstdio>
using namespace std;
const int NULA = 0;
int main (void) {
for( int i = 0; i < 100000000; ++i )
printf( "a" );
return NULA;
}

//cout version
#include <iostream>
using namespace std;
const int NULA = 0;
int main (void) {
std::ios_base::sync_with_stdio(false);

for( int i = 0; i < 100000000; ++i )
cout << "a" ;
return NULA;
}

解决方案

nw wrote:

Hi all,

I''m constantly confronted with the following two techniques, which I
believe often produce less readable code, but I am told are faster
therefore better. Can anyone help me out with counter examples and
arguments?

1. The STL is slow.

More specifically vector. The argument goes like this:

"Multidimensional arrays should be allocated as large contiguous
blocks. This is so that when you are accessing the array and reach the
end of a row, the next row will already be in the cache. You also
don''t need to spend time navigating pointers when accessing the array.
So a 2 dimensional array of size 100x100 should be created like this:

const int xdim=100;
const int ydim=100;

int *myarray = malloc(xdim*ydim*sizeof(int));

and accessed like this:

myarray[xdim*ypos+xpos] = avalue;

Is this argument reasonable? (Sounds reasonable to me, though the
small tests I''ve performed don''t usually show any significant
difference).

To me this syntax looks horrible, am I wrong? Is vector the wrong
container to use? (My usual solution would be a vector<vector<int).
Would using a valarray help?


In normal cases you should use vector as you mentioned.

2. iostream is slow.

I''ve encountered this is work recently. I''d not considered it before,
I like the syntax and don''t do so much IO generally... I''m just now
starting to process terabytes of data, so it''ll become an issue. Is
iostream slow? specifically I encountered the following example
googling around. The stdio version runs in around 1second, the
iostream version takes 8seconds. Is this just down to a poor iostream
implementation? (gcc 4 on OS X). Or are there reasons why iostream is
fundamentally slower for certain operations? Are there things I should
be keeping in mind to speed up io?

// stdio version
#include <cstdio>
using namespace std;
const int NULA = 0;
int main (void) {
for( int i = 0; i < 100000000; ++i )
printf( "a" );
return NULA;
}

//cout version
#include <iostream>
using namespace std;
const int NULA = 0;
int main (void) {
std::ios_base::sync_with_stdio(false);

for( int i = 0; i < 100000000; ++i )
cout << "a" ;
return NULA;
}

I suppose it is an implementation issue. The two codes should undertake
the same time-cost. If you remove
"std::ios_base::sync_with_stdio(false);" the output is slower?
Also check the compiler optimisation switches, they can enhance the
run-time execution.


nw wrote:

>
More specifically vector. The argument goes like this:>
"Multidimensional arrays should be allocated as large contiguous
blocks. This is so that when you are accessing the array and reach the
end of a row, the next row will already be in the cache. You also
don''t need to spend time navigating pointers when accessing the array.
So a 2 dimensional array of size 100x100 should be created like this:

const int xdim=100;
const int ydim=100;

int *myarray = malloc(xdim*ydim*sizeof(int));

and accessed like this:

myarray[xdim*ypos+xpos] = avalue;

Is this argument reasonable? (Sounds reasonable to me, though the
small tests I''ve performed don''t usually show any significant
difference).

To me this syntax looks horrible, am I wrong? Is vector the wrong
container to use? (My usual solution would be a vector<vector<int).
Would using a valarray help?


I think your code is incorrect. Your approach corrected:
#include <cstdlib>

int main()
{
using namespace std;

const int XDIM= 100;

const int YDIM= 200;
int (*my_array)[YDIM]= static_cast<int (*)[200]( malloc(XDIM* YDIM
* **my_array) );

if(my_array== 0)
return EXIT_FAILURE;

for(size_t i= 0; i< XDIM; ++i)
for(size_t j= 0; j< YDIM; ++j)
my_array[i][j]= i+j;
// ...

free(my_array);

// ...
}
The equivalent C++ style:
include <cstdlib>

int main()
{
using namespace std;

const int XDIM= 100;

const int YDIM= 200;
int (*my_array)[YDIM]= new int[XDIM][YDIM];

for(size_t i= 0; i< XDIM; ++i)
for(size_t j= 0; j< YDIM; ++j)
my_array[i][j]= i+j;
}

The proper C++ approach:

#include <cstdlib>
#include <vector>

int main()
{
using namespace std;

const int XDIM= 100;

const int YDIM= 200;

vector<vector<int my_array(XDIM, vector<int>(YDIM));

for(vector<vector<int::size_type i= 0; i< my_array.size(); ++i)
for(vector<int>::size_type j= 0; j< my_array[i].size(); ++j)
my_array[i][j]= i+j;
// ...

// No need to clean up your memory or any other resource
// - RAII (Resource Acquisition Is Initialisation)

}


Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:

The proper C++ approach:

vector<vector<int my_array(XDIM, vector<int>(YDIM));

That is not necessarily the proper approach. The above creates YDIM + 1
separate blocks of code which may, or may not be a good idea.


这篇关于C ++很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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