gnu-misc-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Learning C++ with gnu software?


From: Martin Dickopp
Subject: Re: Learning C++ with gnu software?
Date: Sat, 10 Jul 2004 15:55:00 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Brian Gough <bjg@network-theory.co.uk> writes:

> xeys_00@yahoo.com (Cecil) writes:
>
>> Is there a book or web site that will help me learn C++ in the
>> context of using only gnu command line tools?
>
> There are quite a few command-line gcc, g++ and gdb examples in my
> book, "An Introduction to GCC":
>
>   http://www.network-theory.co.uk/gcc/intro/
>
> It doesn't teach C or C++, just the use of the compilers and
> tools---it's intended to be used as a supplement to a C or C++
> language textbook.
>
> The book is published under the GNU Free Documentation License.

An excellent book, in my opinion.  I have one minor point of criticism,
though.  You write in a C++ example

    std::cout << "Hello, world!" << std::endl;

whereas I would find

    std::cout << "Hello, world!\n";

preferable.  std::endl not only writes a newline character, but also
flushes the stream.  Many programmers seem to follow the bad habit of
using std::endl even in the (common) case where flushing the stream is
not necessary.  Flushing the stream after every line can slow a program
down significantly.  If I run the following program

#include <iostream>
#include <ctime>

int main ()
{
    const std::time_t t0 = std::time (static_cast<std::time_t*> (0));
    for (int i = 0; i < 1000000; ++i)
        std::cout << "Hello, world!" << std::endl;
    const std::time_t t1 = std::time (static_cast<std::time_t*> (0));
    for (int i = 0; i < 1000000; ++i)
        std::cout << "Hello, world!\n";
    std::cout << std::flush;
    const std::time_t t2 = std::time (static_cast<std::time_t*> (0));
    std::clog << std::difftime (t1, t0) << ' '
              << std::difftime (t2, t1) << '\n';
}

with standard output redirected to /dev/null, the first loop takes
96 seconds on my system, while the second takes only 72.  If I include

    std::ios_base::sync_with_stdio (false);

as the first line in main, the first loop takes 29 seconds and the
second takes 4, i.e. not flushing the stream after every line makes
the loop run more than seven times as fast.

Martin

reply via email to

[Prev in Thread] Current Thread [Next in Thread]