为什么用C的strcmp会比C++的compare快这么多????
#include<iostream>
#include<string>
#include <ctime>
using namespace std;
void main()
{
clock_t start;
string strname="luyajun",strfind="liaoyan";
char *cstrname="luyajun",*cstrfind="liaoyan";
start=clock();
const long sleep=10000000;
for(int i=0;i<sleep;i++)
{
strcmp(cstrname,cstrfind);
}
printf("循环%d次,C strcmp所用时间为:%2.4f\n",sleep,(double)(clock()-start)/CLOCKS_PER_SEC);
start=clock();
for(int i=0;i<sleep;i++)
{
strname.compare(strfind);
}
printf("循环%d次,C++String类所用时间为:%2.4f\n",sleep,(double)(clock()-start)/CLOCKS_PER_SEC);
}
上面程序在我的系统上C的strcmp是0.1100,C++的compare是6.2490????
#include<string>
#include <ctime>
using namespace std;
void main()
{
clock_t start;
string strname="luyajun",strfind="liaoyan";
char *cstrname="luyajun",*cstrfind="liaoyan";
start=clock();
const long sleep=10000000;
for(int i=0;i<sleep;i++)
{
strcmp(cstrname,cstrfind);
}
printf("循环%d次,C strcmp所用时间为:%2.4f\n",sleep,(double)(clock()-start)/CLOCKS_PER_SEC);
start=clock();
for(int i=0;i<sleep;i++)
{
strname.compare(strfind);
}
printf("循环%d次,C++String类所用时间为:%2.4f\n",sleep,(double)(clock()-start)/CLOCKS_PER_SEC);
}
上面程序在我的系统上C的strcmp是0.1100,C++的compare是6.2490????
...全文
相关推荐
C/C++中关于std::string的compare陷阱示例详解
主要给大家介绍了关于C/C++中关于std::string的compare陷阱的相关资料,文中先对C/C++中的std::string进行了简单的介绍,通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
C++字符串处理大集合
c++字符串处理大集合,字符串长度、比较、插入、删除等简单使用方法
Windows via C_C++
Chapter 1: Error Handling Chapter 2: Working with Characters and Strings
C_C++语言中函数指针的深入分析与应用
对C/C++语言程序设计中函数指针进行了详细的分析与研究,包括函数指针的概念、定义和调用,并着重通过回调函数与简单消息映射实例阐述函数指针的使用方法和技巧。
C中qsort快速排序使用实例
在学习C++ STL的sort函数,发现C中也存在一个qsort快速排序,要好好学习下C的库函数啊



