热点推荐:ASP.Net | ADO.Net | VB.Net | Web服务器 | Access | MSSQL | MySQL | Oracle | .Net控件 | Win 9x | Win 2000 | Win 2003 | DOS | Unix | 注册表 | 应用其它 | 安装调试 | 基本操作 | 使用技巧 | 系统优化 |故障处理 | 个性风格 | 病毒安全 | 专杀工具
您现在的位置: 中华IT技术网 >> FAQ >> C/C++ >> C语言 >> 正文
全文
使用C++比C慢吗?
作者:1024k    文章来源:Web    更新时间:2007-6-15

我把<string.h>里面的函数全换成了string类的成员函数,把FILE *全换成了fstream类,发现程序运行慢了很多,我不知道是否是换成C++造成的。使用gprof,结果(部分):
granularity: each sample hit covers 8 byte(s) for 0.00% of 29.20 seconds

                                  called/total       parents 
index  %time    self descendents  called+self    name     index
                                  called/total       children

                                                     <spontaneous>
[1]     10.7    3.14        0.00                 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::replace(unsigned long, unsigned long, unsigned long, char) [1]

-----------------------------------------------

                                                     <spontaneous>
[2]      6.7    1.97        0.00                 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_RWpref(void) const  [2]

-----------------------------------------------

                                                     <spontaneous>
[3]      6.5    1.91        0.00                 _RWrwstd::_RWrw_basis<char*, std::allocator<char> >::operator =(const char*&) [3]

-----------------------------------------------

                                                     <spontaneous>
[4]      5.9    1.71        0.00                 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_RWgetRep(unsigned long, unsigned long) [4]

-----------------------------------------------

                                                     <spontaneous>
[5]      5.6    1.65        0.00                 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_RWunLink(void) [5]

-----------------------------------------------

                                                     <spontaneous>
[6]      3.9    1.15        0.00                 std::operator >>(istream&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) [6]

-----------------------------------------------

                                                     <spontaneous>
[7]      3.7    1.07        0.00                 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::length(void) const  [7]

-----------------------------------------------

                                                     <spontaneous>
[8]      3.0    0.87        0.00                 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::append(int, char) [8]

-----------------------------------------------

                                                     <spontaneous>
[9]      2.9    0.84        0.00                 bcd2str(const unsigned char*, char*, int, int, int, int) [9]

-----------------------------------------------

                                                     <spontaneous>
[10]     2.5    0.73        0.00                 _RWrwstd::_RWstring_ref<char, std::char_traits<char>, std::allocator<char> >::_RWremoveReference(void) [10]

运行环境为Compaq Digital ES40,好像SUN和HP也差不多,请教各位大虾,C++比C慢吗?
---------------------------------------------------------------

正确!

c++确实比c要慢,但不应该相差很多,大约在10%---15%之间吧!
因为他牺牲了一些c的底层的东西。同样的道理,c虽然快,但也比不上汇编!

这也是我看文献得来的资料,具体差距你完全可以自己试一试!
---------------------------------------------------------------

C++程序在大多数情况下的执行效率与C相当,除了极少数的虚函数和多冲继承等应用。不过如果你用C来自己处理这些问题,效率可能和C++程序一致,而且自己写的代码更容易出错。
你这里提到的是库的效率问题,C++的流类相比C的stdio来说效率略低,因为里面包含了很多附加的容错处理,如果你偏重效率甚于可维护性,可以考虑使用C库替代。
但是你要确信这里的效率对应用程序的整体效率有显著影响,记住80/20法则,程序80%的时间执行的是20%的代码。

---------------------------------------------------------------

IOStream还会考虑同步的问题。如果关掉同步选项,速度很快很多。

相关文章
最新更新
编辑推荐
热门图片
频道大全
文章阅读排行
周排行
月排行