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:ale...@connect.to
http://mathforum.org/library/view/10978.html
==============================================
>
> "Christoph Rabel" <od...@hal9000.vc-graz.ac.at> wrote in message news:40622d09$0$11742$3b21...@aconews.univie.ac.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++.
Given that this is the microsoft.public.vc.language group, I figured I'd
add a different way of achieving the same thing.
Namely, CopyFile or CopyFileEx...
Much more efficient than rolling your own - most of the file handling
happens in kernel space.
--
Help Support Independent Film
- http://www.cafepress.com/popcornfilms
- http://www.popcornfilms.com/
Any small sample of CopyFile usage in VC++ 7.0 (including compilation via command line too)?
I didn't to succeed in search of such a sample?
Thanks,
--
>
> "Simon Cooke" <simonDoNoTSpam@Popcorn%%%%%films.com> wrote in message news:umVFUhV...@TK2MSFTNGP11.phx.gbl...
>> On Mon, 29 Mar 2004 06:49:13 +0300, Alex Vinokur wrote:
> [snip]
>>> Here are comparative performance of various methods of copying files in C and C++.
>>
>> Given that this is the microsoft.public.vc.language group, I figured I'd
>> add a different way of achieving the same thing.
>>
>> Namely, CopyFile or CopyFileEx...
>>
>> Much more efficient than rolling your own - most of the file handling
>> happens in kernel space.
>>
> [snip]
>
> Any small sample of CopyFile usage in VC++ 7.0 (including compilation via command line too)?
> I didn't to succeed in search of such a sample?
>
> Thanks,
Not that I know of... not that you need one.
It's a reallllllly simple function.
int main(int argc, char*[] argv)
{
if (argc != 3) {
printf("Usage: CopyFile <filename1> <filename2>\n\n");
}
if (!CopyFile(argv[1], argv[2], true))
{
DWORD err = GetLastError();
if (err == ERROR_ACCESS_DENIED) {
printf("File already exists\n");
}
else printf("An error occurred. In a real application, this would be"
" decoded and shown to you here.\n");
}
return 0;
<windows.h>
Schobi
--
Spam...@gmx.de is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
Scott Meyers
I tried to compile my program.
=========
Windows 2000
=========
--------- File foo.cpp ---------
#include <stdio.h>
#include <windows.h>
int main()
{
return 0;
}
------------------
------ Compilation ------
$ cl /clr foo.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 13.00.9466 for .NET Framework
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.
foo.cpp
Microsoft (R) Incremental Linker Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.
/out:foo.exe
foo500.obj
LINK : fatal error LNK1104: cannot open file 'uuid.lib'
-------------------------
For both functions #include <windows.h" and link with kernel32.lib.
PS : All of this is clearly stated in the documentation! :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/copyfile.asp?frame=true
and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp?frame=true
Arnaud
MVP - VC
------ Compilation ------
$ cl /clr /INCREMENTAL:NO foo.cpp /link kernel32.lib
Microsoft (R) C/C++ Optimizing Compiler Version 13.00.9466 for .NET Framework
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.
foo.cpp
Microsoft (R) Incremental Linker Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.
/out:foo.exe
kernel32.lib
foo.obj
LINK : fatal error LNK1104: cannot open file 'uuid.lib'
------------
Something wrong?
> Given that this is the microsoft.public.vc.language group, I figured I'd
> add a different way of achieving the same thing.
>
> Namely, CopyFile or CopyFileEx...
>
> Much more efficient than rolling your own - most of the file handling
> happens in kernel space.
FYI, that's not exactly true. CopyFile is simply a convenience
routine in the Windows Kernel32.dll. It doesn't do anything that you
couldn't do for yourself. It basically opens the source file
(OpenFile/CreateFile), creates a file mapping (CreateFileMapping),
maps the file into your address space (MapViewOfFile) then creates the
destination file (CreateFile) and does a write (WriteFile) directly
from the mapped memory space.
- GH
Do you have the platform SDK installed?
Do you have the correct paths set up?
Have you used the switch on the Linker to stop it from including ALL
default libraries? (some are not needed).
> Do you have the correct paths set up?
Path=
[ irrelevant stuff omitted]
C:\WINNT\system32;
C:\WINNT;
C:\WINNT\System32\Wbem;
C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\include\;
C:\Program Files\Microsoft Visual Studio .NET\Vc7\bin\;
C:\Program Files\Microsoft Visual Studio .NET\Vc7\include\;
C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Lib;
> Have you used the switch on the Linker to stop it from including ALL
> default libraries? (some are not needed).
What does it mean? How to do that?
[snip]
If you have the IDE installed (which it appears you do), perhaps you should
consider using that with one of the project wizards until you find your
feet?