What is the fastest way of copying files

U

utab

Hi there,

I am copying one file into another file character by character.

I wanted to learn if there is a faster way of doing this?

When files get bigger, maybe this can take a lot of time.

Thanks for the replies in advance.
 
I

Ivan Vecerina

: Hi there,
:
: I am copying one file into another file character by character.
:
: I wanted to learn if there is a faster way of doing this?
:
: When files get bigger, maybe this can take a lot of time.
:
: Thanks for the replies in advance.

For C++ streams, the easy way to copy one stream to another
is to send the source's streambuf to the output:
// #include <fstream>
std::ifstream src("source.txt");
std::eek:fstream dst("destination.txt");
dst << src.rdbuf();

This should perform faster than a character-by-character copy,
but actual performance is implementation-dependent.

Of course there is always a faster/more complicated/less portable
way to implement this operation.

hth -Ivan
 
D

Dietmar Kuehl

Ivan said:
For C++ streams, the easy way to copy one stream to another
is to send the source's streambuf to the output:
// #include <fstream>
std::ifstream src("source.txt");
std::eek:fstream dst("destination.txt");
dst << src.rdbuf();

I think the more expressive formulation of the last line is

std::copy(std::istreambuf_iterator<char>(src),
std::istreambuf_iterator<char>(),
std::eek:streambuf_iterator<char>(dst));

However, to get this as efficient as the other version is non-trivial
and effectively requires optimized algorithms for the segmented
sequences.
 
G

Gernot Frisch

However, to get this as efficient as the other version is
non-trivial
and effectively requires optimized algorithms for the segmented
sequences.

Hint: On some compilers the fstream classes are horribly slow in
comparison to fopen/fread/fwrite. For large files you should check if
you can speed up with these.
Also, copyiny byte by byte is generally a bad idea. Read to a buffer
(e.g. 4096 bytes) and write this.
 
D

Dietmar Kuehl

Gernot said:
Hint: On some compilers the fstream classes are horribly slow in
comparison to fopen/fread/fwrite. For large files you should check if
you can speed up with these.
Also, copyiny byte by byte is generally a bad idea. Read to a buffer
(e.g. 4096 bytes) and write this.

Another hint: my own implementation of IOStreams and 'std::copy()'
in particular *is* optimized to implement the algorithm use I have
shown in this thread in a form which is really fast. It is definitely
on par with any other approach you can use on the basis of the
standard C or C++ libraries.
 
A

Alex Vinokur

utab said:
Hi there,

I am copying one file into another file character by character.

I wanted to learn if there is a faster way of doing this?

When files get bigger, maybe this can take a lot of time.

Thanks for the replies in advance.

Look at Simple C/C++ Perfometer: Copying Files (Versions 4.x)
http://groups.google.com/group/perfo/msg/8a74465da4c4e9bb

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
N

noone

If you care to use platform dependent functions then you can use memory
mapped file access. mmap() under unix maps a file to memory or a memory
region back to a file and does it much faster than userland character or
block copies.

To maintain portability you are generally limited to the stdlib routines
though.
 
N

Ni@m

Copying files using block's size equal to the filesystem's scluster
size is possibly is the fastest method.
Or you may try multithreading wih block copying method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top