C ++比C更高效? [英] C++ more efficient than C?

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

问题描述

在学习标准C ++作为新语言中Bjarne Stroustrup声称

正确编写的C ++优于C代码。我将在这里复制他的第一个例子

,这应该是为了演示C ++抽象如何使得b / b $ b代码更易于理解,同时也使它更有效:

===

我能想到的最简单的具体例子是一个程序来查找平均值b / b $ b和一系列双精度浮动的中位数 - 点数从输入中读取

。传统的C风格解决方案是:


#include< stdlib.h>

#include< stdio.h>


int compare(const void * p,const void * q)

{

寄存器double p0 = *(double *)p;

寄存器double q0 = *(double *)q;

if(p0 q0)返回1;

如果(p0< q0)返回 - 1;

返回0;

}


void quit()

{

fprintf(stder,memory exhausted\\\
);

退出(1);

}


int main(int argc,char * argv [])

{

int res = 1000;

char * file = argv [2];


double * buf =(double *)malloc(sizeof(double)* res);

if(buf == 0)quit ();


double median = 0;

double mean = 0;

int n = 0;


FILE * fin = fopen(file," r");

double d;

while(fscanf(fin,"%) lg",& d)== 1){

if(n == res){

res + = res;

buf =(double *)realloc(buf,sizeof(double)* res);

if(buf == 0)quit();

}

buf [n ++] = d;

是什么意思=(n == 1)? d:平均值+(d - 平均值)/ n;

}


qsort(buf,n,sizeof(double),compare);


if(n){

int mid = n / 2;

median =(n%2)? buf [mid] :( buf [mid - 1] + buf [mid])/ 2;

}


printf(" elements of elements = %d,中位数=%g,平均值=%g \ n,n,中位数,

意味着);

免费(buf);

}


比较,这是一个惯用的C ++解决方案:


#include< vector>

#include< fstream>

#include< algorithm>


使用命名空间std;


int main( int argc,char * argv [])

{

char * file = argv [2];

vector< double> buf;


double median = 0;

double mean = 0;


fstream fin(file,ios :: in );

加倍;

而(fin> d){

buf.push_back(d);

mean =(buf.size()== 1)? d:mean +(d - mean)/ buf.size();

}


sort(buf.begin(),buf.end() );


if(buf.size()){

int mid = buf.size()/ 2;

中位数=(buf.size()%2)? buf [mid] :( buf [mid - 1] + buf [mid])/ 2;

}


cout<< 元素数量= << buf.size()

<< " ,median =" <<中值<< ",mean =" <<平均<< ''\ n'';

}

======================

他继续声称,使用5,000,000个元素的样本集,C ++

代码比C代码快4倍。进一步的例子如下:

证明了C ++的优越性,即使在效率领域也是如此。


那么关心效率的C编码员应该转向C ++吗? />

他们反对Stroustroups声明的反对意见吗?

In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that
properly written C++ outperforms C code. I will just copy his first example
here, which is supposed to demonstrate how C++ abstractions do not only make
code easier to understand but also make it more efficient:
===
The simplest specific example I can think of is a program to find the mean
and median of a sequence of double precision floating-point numbers read
from input. A conventional C-style solution would be:

#include <stdlib.h>
#include <stdio.h>

int compare(const void *p, const void *q)
{
register double p0 = * (double *)p;
register double q0 = * (double *)q;
if (p0 q0) return 1;
if (p0 < q0) return -1;
return 0;
}

void quit()
{
fprintf(stder, "memory exhausted\n");
exit(1);
}

int main(int argc, char *argv[])
{
int res = 1000;
char * file = argv[2];

double *buf = (double *) malloc(sizeof (double) *res);
if (buf==0) quit();

double median = 0;
double mean = 0;
int n = 0;

FILE *fin = fopen(file, "r");
double d;
while (fscanf(fin, "%lg", &d) == 1) {
if (n==res) {
res += res;
buf = (double *)realloc(buf, sizeof(double) *res);
if (buf==0) quit();
}
buf[n++] = d;
mean = (n == 1) ? d : mean + (d - mean) / n;
}

qsort(buf, n, sizeof(double), compare);

if (n) {
int mid = n/2;
median = (n%2) ? buf[mid] : (buf[mid - 1] + buf[mid]) / 2;
}

printf("number of elements = %d, median = %g, mean = %g\n", n, median,
mean);
free(buf);
}

To compare, here is an idiomatic C++ solution:

#include<vector>
#include<fstream>
#include<algorithm>

using namespace std;

int main(int argc, char * argv[])
{
char *file = argv[2];
vector<double>buf;

double median = 0;
double mean = 0;

fstream fin(file, ios::in);
double d;
while (fin >d) {
buf.push_back(d);
mean = (buf.size() == 1) ? d : mean + (d - mean) / buf.size();
}

sort(buf.begin(), buf.end());

if (buf.size()) {
int mid = buf.size() / 2;
median = (buf.size() %2) ? buf[mid] : (buf[mid - 1] + buf[mid]) / 2;
}

cout << "number of elements = " << buf.size()
<< " , median = " << median << ", mean = " << mean << ''\n'';
}
======================

He goes on to claim that, using a sample set of 5,000,000 elements, the C++
code is more than 4 times faster than the C code. Further examples follow to
prove the superiority of C++ even in the realm of efficiency.

So C coders who care about efficiency should switch to C++?

Are their any counter-arguments against Stroustroups claim?

推荐答案

copx写道:
copx wrote:

>

那么关心效率的C编码员应该切换到C ++?
>
So C coders who care about efficiency should switch to C++?



你是否想要开始一场火焰战争?


在某些情况下(通常内联函数模板可以用
替换带有void *参数的函数)C ++将要快点因为C ++标准库包含了C标准库,因此C ++代码

应该永远不会慢。


-

Ian Collins。

Are you trying to start a flame war?

In some instances (typically where inlined function templates can
replace functions with void* parameters) C++ will be faster. Because
the C++ standard library incorporates the C standard library, C++ code
should never have to be slower.

--
Ian Collins.




" copx" < co ** @ gazeta.plschrieb im Newsbeitrag

news:ft ********** @ inews.gazeta.pl ...

"copx" <co**@gazeta.plschrieb im Newsbeitrag
news:ft**********@inews.gazeta.pl...

In学习标准C ++作为新语言 Bjarne Stroustrup声称

正确编写的C ++优于C代码。我将在这里复制他的第一个

示例,这应该是为了演示C ++抽象不会如何使b $ b $仅使代码更容易理解,但也使它更有效:
In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that
properly written C++ outperforms C code. I will just copy his first
example here, which is supposed to demonstrate how C++ abstractions do not
only make code easier to understand but also make it more efficient:



[snip]


我手工复制了这些例子,两个错别字潜入:在C示例中它

应该是stderr而不是stder,而C ++示例缺少最终的

结束括号。

[snip]

I copied the examples by hand, and two typos sneaked in: in the C example it
should be "stderr" instead of "stder", and the C++ example lacks a final
closing bracket.




"伊恩柯林斯 < ia ****** @ hotmail.comschrieb im Newsbeitrag

新闻:65 ************* @ mid.individual.net ...

"Ian Collins" <ia******@hotmail.comschrieb im Newsbeitrag
news:65*************@mid.individual.net...

copx写道:
copx wrote:

>>
那么关心效率的C编码员应该切换到C ++?
>>
So C coders who care about efficiency should switch to C++?



你想开始一场火焰战吗?

Are you trying to start a flame war?



不,如果这是我的意图,我会将其转换为

comp.lang.c ++以获得最大效果! ;)

No, if that were my intention, I would have crossposted this to
comp.lang.c++ for maximum effect! ;)


在某些情况下(通常内联函数模板可以使用void *参数替换函数
)C ++会更快。因为C ++标准库包含C标准库,所以C ++代码

应该永远不会慢。
In some instances (typically where inlined function templates can
replace functions with void* parameters) C++ will be faster. Because
the C++ standard library incorporates the C standard library, C++ code
should never have to be slower.



当然,但有趣的说法是你不应该使用C风格的

代码而是现代C ++抽象,因为它们不是只有更具可读性和b $ b和富有表现力,但也更有效率。第二部分(它们实际上是
效率更高)对我来说是新闻。

好​​吧,内联函数模板优于void *函数的东西

当然有道理。也许我最终应该继续前进。毕竟。

但是,我用当前版本的GCC(在

Win32-x86上)编译了两个例子,并且至少有一个C赢得的区域:代码尺寸。解析的可执行文件之间的大小差别很大:C代码= 5KB,

C ++代码=超过400KB! (两个二进制文件都针对大小进行了优化并被剥离了!)


Of course, but the interesting claim here is that you should NOT use C style
code but modern C++ abstractions, because they are not only more readable
and expressive, but also more efficient. The second part (they actually
being more efficient) was news to me.
Well, the inlined function templates superiour to void * functions thing
certainly makes sense. Maybe I should finally "move on" after all.
However, I compiled both examples with the current version of GCC (on
Win32-x86), and there is at least one area where C won: code size. The size
difference between the resoluting executables was grotesque: C code =5KB,
C++ code =over 400KB! (both binaries optimized for size and stripped!)



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

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