- #include <iostream>
- #include <string>
- #include <vector>
- #include <bitset>
- #include <cstddef>
- #include <ctime>
- using namespace std;
- int main()
- {
- const char *pc="A very long string ,test C and C++ speed!";
- const size_t len=strlen(pc);
- clock_t t1,t2;
- t1=clock();
- for(size_t ix=0;ix!=100000;++ix)
- {
- char *pc2=new char[len+1];
- strcpy(pc2,pc);
- if(strcmp(pc,pc2))
- ;
- delete []pc2;
- }
- t2=clock();
- std::cout<<t2-t1<<endl;
- string s("A very long string ,test C and C++ speed!");
- clock_t m1,m2;
- m1=clock();
- for(size_t ix=0;ix!=100000;++ix)
- {
- string s1(s);
- if(s1!=s)
- ;
- }
- m2=clock();
- std::cout<<m2-m1<<endl;
- }
C字符串和C++速度比较
最新推荐文章于 2026-06-25 09:07:43 发布
本文通过一个字符串复制和比较的实例,使用C和C++两种语言进行编码,并利用clock函数测量执行时间,来直观展示C语言与C++在运行效率上的区别。

5315

被折叠的 条评论
为什么被折叠?



