delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/03/29/00:01:11

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "Alex Vinokur" <alexvn AT foot DOT com>
Newsgroups: comp.lang.c++,borland.public.cppbuilder.language.cpp,comp.os.msdos.djgpp,gnu.g++.help,microsoft.public.vc.language
Subject: Re: efficiency in copying files
Followup-To: comp.lang.c++
Date: Mon, 29 Mar 2004 06:49:13 +0300
Lines: 503
Message-ID: <c489sa$2fstv9$1@ID-79865.news.uni-berlin.de>
References: <ba2f9c57 DOT 0403241049 DOT 76634d40 AT posting DOT google DOT com> <40622d09$0$11742$3b214f66 AT aconews DOT univie DOT ac DOT at>
NNTP-Posting-Host: 82-166-72-164.barak.net.il (82.166.72.164)
X-Trace: news.uni-berlin.de 1080535758 83785705 I 82.166.72.164 ([79865])
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Christoph Rabel" <odie AT hal9000 DOT vc-graz DOT ac DOT at> wrote in message news:40622d09$0$11742$3b214f66 AT aconews DOT univie DOT ac DOT at...
> fabio de francesco wrote:
> >
> > My configuration is linux2.6.4, gcc3.2.3, libc2.3.2 and stdlibc++3.0.
> >
> > I have some problems in a simple copy from a 2795811 byte long binary
> > file to a new one.
> > In the following you can find a C program (ref.1) that accomplishes
> > that task in msec.50 (average time of 10 different executions).
> >
> > On the same computer and configuration I try to do the same with a
> > similar program in C++ (ref.2), but the task is done in an average of
> > msec.120.
> > That is an average of 140% more execution time.
> >
> > What am I missing? How can I write a simple C++ program that can do
> > the same task as the original C program with the same efficency?
>
> Now, there is a overhead for using streams instead of the
> old C functions. But, as I recall streams are very slow with
> gcc 3.x and this behaviour is considered a bug.
>
[snip]


Here are comparative performance of various methods of copying files in C and C++.



        Copying files : input to output
        ===============================

        C/C++ Performance Tests
        =======================
        Using C/C++ Program Perfometer
        http://sourceforge.net/projects/cpp-perfometer
        http://alexvn.freeservers.com/s1/perfometer.html



    Environment
    -----------
    Windows 2000 Professional
    Intel(R) Celeron(R) CPU 1.70 GHz

    Compilers
    ---------
    * GNU g++ 3.3.1 (cygwin);    DLLs : ctgwin1.dll, kernel32.dll, ntdll.dll
    * GNU g++ 3.3.1 (mingw);     DLLs : msvcrt.dll, kernel32.dll, ntdll.dll
    * GNU g++ 3.3.2 (djgpp);     DLLs : No
    * Microsoft C++ 13.00.9466;  DLLs : kernel32.dll, ntdll.dll
    * Borland C++ 5.5.1;         DLLs : kernel32.dll, ntdll.dll, user32.dll, gdi32.dll

    Testsuites
    ----------
    C-1   : Functions getc() and putc()
    C-2   : Functions fgetc() and fputc()
    C-3   : Functions fread() and fwrite()
    CPP-1 : Operators >> and <<
    CPP-2 : Methods get() and put()
    CPP-3 : Methods sbumpc() and sputc()
    CPP-4 : Method sbumpc() and operator <<
    CPP-5 : Method rdbuf() and operator <<
    CPP-6 : Methods read() and write() with const buffer
    CPP-7 : Methods read() and write() with max buffer



    #################################################
    Stream I/O performance tests below are based
    on the article "Stream I/O"
    presented at http://www.glenmccl.com/strm_cmp.htm
    by Glen McCluskey & Associates LLC
    #################################################



===================== Methods of copying : BEGIN =====================

ifstream in_fs;
ofstream out_fs;

FILE*  in_fp;
FILE*  out_fp;

char ch;
int  ich;

char   buf[4096];
size_t nread;

char* mbuf = new char [file_size];




    ### Method C-1 : Functions getc() and putc()
    -----------------------------------------------------
    while ((ich = getc(in_fp)) != EOF) putc(ich, out_fp);
    -----------------------------------------------------


    ### Method C-2 : Functions fgetc() and fputc()
    -------------------------------------------------------
    while ((ich = fgetc(in_fp)) != EOF) fputc(ich, out_fp);
    -------------------------------------------------------


    ### Method C-3 : Functions fread() and fwrite()
    -------------------------------------------------------
    while ((nread = fread(buf, sizeof(char), sizeof(buf), in_fp)) > 0)
    {
      fwrite(buf, sizeof(char), nread, out_fp);
    }
    -------------------------------------------------------




    ### Method CPP-1 : Operators >> and <<
    ---------------------------------
    in_fs.unsetf(ios::skipws);
    while (in_fs >> ch) out_fs << ch;
    ---------------------------------


    ### Method CPP-2 : Methods get() and put()
    -------------------------------------
    while (in_fs.get(ch)) out_fs.put(ch);
    -------------------------------------


    ### Method CPP-3 : Methods sbumpc() and sputc()
    ------------------------------------------------------------------------
    while ((ch = in_fs.rdbuf()->sbumpc()) != EOF) out_fs.rdbuf()->sputc(ch);
    ------------------------------------------------------------------------


    ### Method CPP-4 : Method sbumpc() and operator <<
    -------------------------------
    ch = in_fs.rdbuf()->sbumpc();
    out_fs << ch;
    while (ch != EOF)
    {
      out_fs << in_fs.rdbuf();
      ch = in_fs.rdbuf()->sbumpc();
    }
    -------------------------------


    ### Method CPP-5 : Method rdbuf() and operator <<
    ------------------------
    out_fs << in_fs.rdbuf();
    ------------------------


    ### Method CPP-6 : Methods read() and write() with const buffer
    ------------------------
    while (!in_fs.eof())
    {
      in_fs.read (buf, sizeof(buf));
      out_fs.write (buf,in_fs.gcount());
    }
    ------------------------


    ### Method CPP-7 : Methods read() and write() with max buffer
    ------------------------
    in_fs.read   (mbuf, file_size);
    out_fs.write (mbuf, file_size);
    ------------------------



===================== Methods of copying : END =======================




================ Performance tests : BEGIN ================


       #==========================================================
       # Comparison : copying files : input to output
       #----------------------------------------------------------
       #   Resource Name       : CPU-time used
       #   Total repetitions   : 1000
       #   Performance metrics : milliseconds / 100 repetitions
       #==========================================================




                    --- Summary test results ---
                    ============================
                    Compiler     : GNU g++ 3.3.1 (cygwin)
                    Optimization : No
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     4 |    11 |    85 |
  | C-2   | Functions fgetc() and fputc()                |     4 |    11 |    88 |
  | C-3   | Functions fread() and fwrite()               |     4 |     9 |    69 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |    43 |   384 |  3691 |
  | CPP-2 | Methods get() and put()                      |    25 |   196 |  1866 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     8 |    23 |   195 |
  | CPP-4 | Method  sbumpc() and operator <<             |     7 |    12 |    80 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     7 |    11 |    78 |
  | CPP-6 | Methods read() and write() with const buffer |     7 |    11 |    80 |
  | CPP-7 | Methods read() and write() with max buffer   |     7 |    11 |    69 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : GNU g++ 3.3.1 (cygwin)
                    Optimization : O2
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     5 |    10 |    91 |
  | C-2   | Functions fgetc() and fputc()                |     5 |    11 |    94 |
  | C-3   | Functions fread() and fwrite()               |     5 |     9 |    71 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |    45 |   399 |  3775 |
  | CPP-2 | Methods get() and put()                      |    26 |   216 |  1913 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     7 |    21 |   169 |
  | CPP-4 | Method  sbumpc() and operator <<             |     7 |    13 |    93 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     7 |    13 |    89 |
  | CPP-6 | Methods read() and write() with const buffer |     7 |    13 |    85 |
  | CPP-7 | Methods read() and write() with max buffer   |     7 |    13 |    83 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : GNU g++ 3.3.1 (mingw)
                    Optimization : No
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     3 |     7 |    66 |
  | C-2   | Functions fgetc() and fputc()                |     4 |    28 |   276 |
  | C-3   | Functions fread() and fwrite()               |     2 |     3 |    24 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |     9 |    69 |   671 |
  | CPP-2 | Methods get() and put()                      |     7 |    40 |   328 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     4 |    14 |   108 |
  | CPP-4 | Method  sbumpc() and operator <<             |     4 |     6 |    38 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     4 |     6 |    38 |
  | CPP-6 | Methods read() and write() with const buffer |     3 |     6 |    39 |
  | CPP-7 | Methods read() and write() with max buffer   |     3 |     5 |    37 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : GNU g++ 3.3.1 (mingw)
                    Optimization : O2
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     2 |     5 |    36 |
  | C-2   | Functions fgetc() and fputc()                |     4 |    28 |   237 |
  | C-3   | Functions fread() and fwrite()               |     2 |     4 |    20 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |     9 |    65 |   545 |
  | CPP-2 | Methods get() and put()                      |     6 |    37 |   304 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     4 |    12 |    98 |
  | CPP-4 | Method  sbumpc() and operator <<             |     4 |     6 |    37 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     3 |     5 |    37 |
  | CPP-6 | Methods read() and write() with const buffer |     3 |     6 |    38 |
  | CPP-7 | Methods read() and write() with max buffer   |     3 |     5 |    38 |
  --------------------------------------------------------------------------------





                    --- Summary test results ---
                    ============================
                    Compiler     : GNU g++ 3.3.2 (djgpp)
                    Optimization : No
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     7 |    19 |    77 |
  | C-2   | Functions fgetc() and fputc()                |    11 |    18 |    86 |
  | C-3   | Functions fread() and fwrite()               |     9 |    12 |    38 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |    18 |    54 |   435 |
  | CPP-2 | Methods get() and put()                      |    18 |    32 |   200 |
  | CPP-3 | Methods sbumpc() and sputc()                 |    13 |    23 |   116 |
  | CPP-4 | Method  sbumpc() and operator <<             |    14 |    15 |    49 |
  | CPP-5 | Method  rdbuf()  and operator <<             |    15 |    13 |    47 |
  | CPP-6 | Methods read() and write() with const buffer |    16 |    16 |    51 |
  | CPP-7 | Methods read() and write() with max buffer   |    13 |    15 |    52 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : GNU g++ 3.3.2 (djgpp)
                    Optimization : O2
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |    11 |    18 |    77 |
  | C-2   | Functions fgetc() and fputc()                |    11 |    16 |    78 |
  | C-3   | Functions fread() and fwrite()               |    11 |    12 |    44 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |    22 |    55 |   430 |
  | CPP-2 | Methods get() and put()                      |    19 |    33 |   224 |
  | CPP-3 | Methods sbumpc() and sputc()                 |    16 |    22 |   107 |
  | CPP-4 | Method  sbumpc() and operator <<             |    16 |    16 |    49 |
  | CPP-5 | Method  rdbuf()  and operator <<             |    15 |    16 |    49 |
  | CPP-6 | Methods read() and write() with const buffer |    15 |    16 |    53 |
  | CPP-7 | Methods read() and write() with max buffer   |    14 |    16 |    55 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : Microsoft C++ 13.00.9466
                    Optimization : Od
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     3 |     8 |    73 |
  | C-2   | Functions fgetc() and fputc()                |     3 |    10 |    90 |
  | C-3   | Functions fread() and fwrite()               |     3 |     5 |    29 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |    16 |   131 |  1163 |
  | CPP-2 | Methods get() and put()                      |    16 |   129 |  1196 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     5 |    27 |   239 |
  | CPP-4 | Method  sbumpc() and operator <<             |     8 |    51 |   456 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     8 |    51 |   476 |
  | CPP-6 | Methods read() and write() with const buffer |     3 |     6 |    34 |
  | CPP-7 | Methods read() and write() with max buffer   |     3 |     6 |    34 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : Microsoft C++ 13.00.9466
                    Optimization : O2
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     2 |     5 |    39 |
  | C-2   | Functions fgetc() and fputc()                |     2 |     7 |    58 |
  | C-3   | Functions fread() and fwrite()               |     2 |     4 |    28 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |    15 |   122 |  1173 |
  | CPP-2 | Methods get() and put()                      |    14 |   124 |  1121 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     5 |    27 |   197 |
  | CPP-4 | Method  sbumpc() and operator <<             |     8 |    58 |   426 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     8 |    55 |   470 |
  | CPP-6 | Methods read() and write() with const buffer |     3 |     6 |    33 |
  | CPP-7 | Methods read() and write() with max buffer   |     3 |     5 |    33 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : Microsoft C++ 13.00.9466
                    Optimization : Ox
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     2 |     5 |    35 |
  | C-2   | Functions fgetc() and fputc()                |     2 |     6 |    51 |
  | C-3   | Functions fread() and fwrite()               |     2 |     4 |    25 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |    14 |   111 |  1200 |
  | CPP-2 | Methods get() and put()                      |    13 |   105 |  1056 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     5 |    22 |   220 |
  | CPP-4 | Method  sbumpc() and operator <<             |     7 |    44 |   417 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     7 |    44 |   415 |
  | CPP-6 | Methods read() and write() with const buffer |     3 |     5 |    30 |
  | CPP-7 | Methods read() and write() with max buffer   |     3 |     5 |    30 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : Borland C++ 5.5.1
                    Optimization : Od
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     2 |     5 |    51 |
  | C-2   | Functions fgetc() and fputc()                |     3 |    22 |   216 |
  | C-3   | Functions fread() and fwrite()               |     2 |     3 |    24 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |     8 |    47 |   456 |
  | CPP-2 | Methods get() and put()                      |     7 |    43 |   361 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     4 |    12 |    88 |
  | CPP-4 | Method  sbumpc() and operator <<             |     4 |    11 |    82 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     4 |    11 |    80 |
  | CPP-6 | Methods read() and write() with const buffer |     4 |    10 |    73 |
  | CPP-7 | Methods read() and write() with max buffer   |     3 |    10 |    66 |
  --------------------------------------------------------------------------------




                    --- Summary test results ---
                    ============================
                    Compiler     : Borland C++ 5.5.1
                    Optimization : O2
                    ============================
  --------------------------------------------------------------------------------
  |       |                                              |   CPU time used for   |
  |  No.  |                    Method                    |       file size       |
  |       |                                              |-----------------------|
  |       |                                              |  100  | 1000  | 10000 |
  |------------------------------------------------------------------------------|
  | C-1   | Functions getc() and putc()                  |     2 |     5 |    36 |
  | C-2   | Functions fgetc() and fputc()                |     4 |    28 |   237 |
  | C-3   | Functions fread() and fwrite()               |     2 |     4 |    20 |
  |       |                                              |       |       |       |
  | CPP-1 | Operators >> and <<                          |     9 |    65 |   545 |
  | CPP-2 | Methods get() and put()                      |     6 |    37 |   304 |
  | CPP-3 | Methods sbumpc() and sputc()                 |     4 |    12 |    98 |
  | CPP-4 | Method  sbumpc() and operator <<             |     4 |     6 |    37 |
  | CPP-5 | Method  rdbuf()  and operator <<             |     3 |     5 |    37 |
  | CPP-6 | Methods read() and write() with const buffer |     3 |     6 |    38 |
  | CPP-7 | Methods read() and write() with max buffer   |     3 |     5 |    38 |
  --------------------------------------------------------------------------------




================ Performance tests : END ==================


   ==============================================
   Alex Vinokur
     mailto:alexvn AT connect DOT to
     http://mathforum.org/library/view/10978.html
   ==============================================



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019