开发者

Cause for this difference..from C++ to C?

开发者 https://www.devze.com 2023-02-14 09:11 出处:网络
i wonder why is this happening all the time...!! I wrote two programs one in c and the other in c++.. Both performs the same action. i.e prints numbers from 1 to 2000000. Also i am setting the timer a

i wonder why is this happening all the time...!! I wrote two programs one in c and the other in c++.. Both performs the same action. i.e prints numbers from 1 to 2000000. Also i am setting the timer at the begtinning of execution.. and after printing all the numbers time elapsed is also printed.. The time elapsed for a c++ program is always greater than for a c program. i feel the difference in time is significant. I am curious to know what is the cause for this..????..

Here are the 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 c program by issuing the command

g++ iotest.c

Execution gives

1

.

.

2000000

Time Elapsed: 5.410000(Not always same..)

Executing the second program

1

.

.

2000000

Time e开发者_Go百科lapsed: 5.81(Not always same..)


The difference between the two programs is that the C++ version uses endl, which not only inserts a newline but flushes the buffer. The slow part of doing any output is flushing the buffer.

Both programs would probably be about the same speed if you made your C++ program use

count << i << "\n";

The following two programs achieve a similar execution time:

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;
}

Compile with: gcc -O3 -std=c99 a.c

C++-program (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;
}

Compile with g++ -O3 b.cpp


You've started the clock at static-initialisation time, and your C++ standard library may have more work to do during static initialisation.

You're also flushing your stream repeatedly, and iostreams are a bit slow.

Do this:

#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;
}

A proper benchmark would involve you running this program a number of times and then taking the average of the results. The results you have are not at all reliable.

(Also notice how I changed time.h to its C++ counterpart, ctime.)


Using cout << endl will force a flush. Likely the difference will be lower if you use cout << "\n"

That said, iostreams are (in my personal opinion) not the most effective mechanism, and I'd expect the printf to still be faster. If output speed is the performance bottleneck for an application, you should likely fwrite to stdout, otherwise just stick to the typesafe cout.


It's because std::cout is designed differently than printf(). What you lose in performance you gain back in maintainability (e.g. type safety, flexibility in usage, structured syntax, etc). The iostream library is one of the bulkiest parts of the C++ standard library, so this is not surprising.

Take a look at the assembly output and you'll see this with your own eyes.


C++ is somewhat higher-level, the way the streams work seems very different from printf and the C and C++ compilers will both produce different code.


I believe that the bottleneck is in the formatting of the data. I changed Gabe's example to output a constant array of characters to the console (to minimize effects due to formatting of integers):

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;
}

Compilation statements:

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

Executable sizes:

# 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

Performance (run on a standalone XP, with minimal OS and applications):

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

The C++ program may be running faster because it is invoking methods tailored to the data. The printf still has to scan the format string before it prints.

Before any substantial conclusions are drawn, one should profile "fwrite" versus "cout.write()" to get a baseline. This would be the time to subtract from other comparisons since it represents the I/O overhead of the system. (I'm assuming that these functions use minimal code to output to the console).


Unfortunately, the reason is that streams are slower than printf()

You may consider putting a breakpoint and see whats going on underneath those calls ;)

+

The pros and cons of using cout vs printf is already took humanity to spend a decade. I didn’t want to copy past some sort of a long discussion like the one below: http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/77637c6bf362a01c?pli=1

The reality is that cout is slower (if you want me to be precise I can put here: in general) than printf.
Yes, streams are designed to adopt and benefit from higher level software design paradigms.
Yes, they are type safe.
Yes, they are more flexible.
Yes, there is bunch of extremely useful stuff like boost's lexical_cast depending on them.
Yet, give me as many -1 us you like guys, but still it won't change the fact that streams are slower than printf ;)

You may ask, why?
C++ cout printing slowly
cout or printf which of the two has a faster execution speed C++?
mixing cout and printf for faster output

0

精彩评论

暂无评论...
验证码 换一张
取 消