The reason for this difference ... from C ++ to C?

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 ..)

+5
7

, ++ endl, , . - .

, , , ++

count << i << "\n";

:

C-program (a.c):

#include <stdio.h>
#include <time.h>

int main()
{
    clock_t start=clock();
    for (int i=0; i<2000000; i++) printf("%d\n",i);
    clock_t end=clock();

    fprintf(stderr, "Time Elapsed: %f\n",((double)end-start)/CLOCKS_PER_SEC);
    return 0;
}

: gcc -O3 -std=c99 a.c

++ - (b.cpp):

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    clock_t start=clock();
    for (int i=0;i<2000000;i++) cout << i << '\n';
    clock_t end=clock();

    cerr << "Time elapsed " << ((double)end-start)/CLOCKS_PER_SEC << endl;

    return 0;
}

g++ -O3 b.cpp

+18

, ++ .

, iostreams .

:

#include <iostream>
#include <ctime>

int main()
{
    std::clock_t start = std::clock();
    for (int i=0;i<2000000;i++)
       std::cout << i << '\n';
    std::cout << "Time elapsed " << (static_cast<double>(std::clock()-start)/CLOCKS_PER_SEC) << std::endl;
    return 0;
}

, , . .

( , time.h ++, ctime.)

+8

cout << endl . , , cout << "\n"

, iostreams ( ) , , printf . , , , , cout.

+6

, std::cout , printf(). , , (, , , ..). iostream ++, .

, .

+1

++ , , , printf, C ++ .

+1

, . Gabe, ( - ):

a.c

include <stdio.h>
#include <time.h>

int main()
{
    static const char   hello[] = "hello\n";
    clock_t start=clock();
    for (int i=0; i<2000000; i++) printf("%s",hello);
    clock_t end=clock();

    fprintf(stderr, "Time Elapsed: %f\n",((double)end-start)/CLOCKS_PER_SEC);
    return 0;
}

b.cpp:

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    static const char   hello[] = "hello\n";
    clock_t start=clock();
    for (int i=0;i<2000000;i++) cout << hello;
    clock_t end=clock();

    cerr << "Time elapsed " << ((double)end-start)/CLOCKS_PER_SEC << endl;

    return 0;
}

:

gcc -mno-cygwin -O3 -std=c99 a.c
g++ -mno-cygwin -O3 -o b.exe b.cpp

:

# ls -al *.exe
-rwxr-xr-x 1 Thomas root  49052 Mar  3 12:00 a.exe
-rwxr-xr-x 1 Thomas root 503195 Mar  3 12:01 b.exe

( XP ):

a.c:  Time elapsed: 187.359
b.cpp: Time elapsed: 120.718

++ , , . printf - .

- , "fwrite" "cout.write()", . , - . ( , ).

+1

, , , printf()

, ;)

+

cout vs printf . , : http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/77637c6bf362a01c?pli=1

, cout ( , , : ), printf.
, .
, .
, . , , boost lexical_cast.
, , , , , printf;)

, ? ++ cout
cout printf, ++?
cout printf

0
source

All Articles