I wonder why this happens all the time ... !! I wrote two programs, one in c and the other in C ++. Both perform the same action. ie prints numbers from 1 to 2,000,000. I also set a timer at the beginning of execution .. and after printing all the numbered numbers are also printed. The elapsed time for a C ++ program is always longer than for a c program. I feel that the time difference is significant. I am curious to know what is the reason for this .. ???? ..
Here are two programs
//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;
}
//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;
}
// ver C ++ 4.3.2 Compiling a c-program by issuing a command
g ++ iotest.c
Execution gives
1
.
.
2,000,000
Elapsed time: 5.410000 (not always the same ..)
Execution of the second program
1
.
.
2,000,000
Elapsed time: 5.81 (not always the same ..)