我想知道为什么会一直这样...... !! 我在c中编写了两个程序,在c ++中编写了另一个程序.两者都执行相同的操作.即打印1到2000000之间的数字.此外,我在执行开始时设置计时器..并且在打印之后还打印所有经过的时间.c ++程序所用的时间总是大于交流程序.我觉得时间差异很大.我很想知道这是什么原因.. ???? ..
这是两个程序
//iotest.c
#include<stdio.h>
#include<time.h>
clock_t start=clock();
int main()
{
for(int i=0;i<2000000;i++)
printf("%d\n",i);
printf("Time Elapsed: %f\n",((double)clock()-start)/CLOCKS_PER_SEC);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
//iotest.cpp
#include<iostream>
#include<time.h>
using namespace std;
clock_t start=clock();
int main()
{
for(int i=0;i<2000000;i++)
cout<<i<<endl;
cout<<"Time elapsed "<<((double)clock()-start)/CLOCKS_PER_SEC<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
// ver C++ 4.3.2通过发出命令编译c程序
g ++ iotest.c
执行给出
1
.
.
2000000
经过的时间:5.410000(并不总是相同..)
执行第二个程序
1
.
.
2000000
时间流逝:5.81(并非总是相同..)