Comparison between using MMAP () and using read () write () to copy objects

Source: Internet
Author: User

Recently, the instructor said this: MMAP () memory ing can be used to copy files, and the speed is significantly faster than normal file copying.
I tried to copy two files at a time.
First, check the Code:

# Include <sys/types. h>
# Include
<Sys/STAT. h>
# Include <fcntl. h>
# Include
<Unistd. h>
# Include <sys/Mman. h>
# Include
<Stdlib. h>
# Include <stdio. h>
# Include <errno. h>
# Include
<Sys/time. h>
# Include <string. h>

# Define
Buffer_size 1

Void my_copy1 ()
{
Int fin, fout;

Void * start;
Void * end;
Struct stat Sb;
If (fin =
Open ("file. In", o_rdonly) <0 ){
Perror ("Open error ");

Exit (exit_failure );
}
If (fout = open (
"File. Out", o_rdwr | o_creat | o_trunc, 00600) <0 ){

Perror ("write error ");
Exit (exit_failure );
}


Fstat (FIN, & SB );


If (lseek (fout, SB. st_size-1, seek_set) <0 ){

Exit (exit_failure );
}
If (write (fout, & SB, 1 )! = 1 ){

Exit (exit_failure );
}

Start =
MMAP (null, SB. st_size, prot_read, map_private, FIN, 0 );
If (START =
Map_failed)
Return;

End =
MMAP (0, (size_t) sb. st_size, prot_write, map_shared, fout, 0 );
If (end
= Map_failed ){
Perror ("MMAP target ");
Exit (
Exit_failure );
}


Memcpy (end, start, (size_t) sb. st_size );
Munmap (START, SB. st_size );
// Close the ing
Close (FIN );
Close (fout );
Return;
}

Void
My_copy2 ()
{
Int fin, fout;
Int bytes_read, bytes_write;

Char buffer [buffer_size];
Char * PTR;
If (fin =
Open ("file. In", o_rdonly) <0 ){
Perror ("Open error ");

Exit (exit_failure );
}
If (fout = open (
"File. Out", o_rdwr | o_creat | o_trunc, 00700) <0 ){

Perror ("write error ");
Exit (exit_failure );
}


While (bytes_read = read (FIN, buffer, buffer_size )){

If (bytes_read =-1) & (errno! = Eintr ))
Break;

Else if (bytes_read> 0 ){
PTR = buffer;

While (bytes_write = write (fout, PTR, bytes_read )){

If (bytes_write =-1) & (errno! = Eintr ))

Break;
Else if (bytes_write = bytes_read)

Break;
Else if (bytes_write> 0 ){

PTR + = bytes_write;

Bytes_read-= bytes_write;
}
}

If (bytes_write =-1)
Break;
}

}

Close (FIN );
Close (fout );
Return;
}

Main ()
{

Struct timeval TV;
Struct timezone TZ;
Int
Time_start, time_end;
Gettimeofday (& TV, & Tz );

Time_start = (INT) TV. TV _usec;
My_copy1 ();

Printf ("/ndone./n ");
Gettimeofday (& TV, & Tz );

Time_end = (INT) TV. TV _usec;
Printf ("using/" MMAP ()/"to copy
Costs % d microseconds/N ", time_end-time_start );


Gettimeofday (& TV, & Tz );
Time_start = (INT) TV. TV _usec;

My_copy2 ();
Gettimeofday (& TV, & Tz );
Time_end =
(INT) TV. TV _usec;
Printf ("using/" Read () and write ()/"to copy
Costs % d microseconds/N ", time_end-time_start );
}

The code is not very difficult.
Some linuxc functions are used between them. If you do not understand them, you can view relevant information on your own. I want to use either of the following methods:
The implementation of different copies compares the time spent and
Some of your own ideas, buffer_size can be used to debug the program
You can change a number to indicate the number of characters read from the file once using the READ function. Of course, I emphasized
There is a reason for this.
If buffer_size is small, the final result is very different. For example, my
When buffer_size = 1, my running results are as follows:
Zhou @ ZHOU :~ /Linuxc/file/mmcopy $
./MMAP

Done.

Using "MMAP ()" to copy the costs 591
Microseconds
Using "Read () and write ()" to copy costs 505337
Microseconds
Zhou @ ZHOU :~ /Linuxc/file/mmcopy $
The two are not an order of magnitude. Change the number below
Buffer_size = 10000
My running status is as follows:
Zhou @ ZHOU :~ /Linuxc/file/mmcopy $./MMAP

Done.

Using
"MMAP ()" to copy costs 594 microseconds
Using "Read () and write ()"
To copy costs 585 microseconds
Zhou @ ZHOU :~ /Linuxc/file/mmcopy $
This
The time consumed by the two instances is very close. If buffer_size is defined as large, the read () write () method will
Very fast,. If you want
The copied file is very small. It only contains 100 bytes, But you apply for 10000 bytes each time.
Memory is a waste. This is the advantage of MMAP ().
Storage, and the speed is quite fast.
In my understanding, MMAP first maps all the content of the file to be copied to the memory, and then writes it to the object
Text
The total disk operation is performed twice, and read () write () is different, it will be defined according to your buffer_size, and then will be executed
(Total number of bytes of the file content
/Buffer_size) * two disk operations, which wastes a lot of time. So

Okay, that's all. If you have any questions, leave a message,
Discuss with each other. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.