Analysis of execution of memory memcpy Efficiency Test Based on C ++

Source: Internet
Author: User

During the memcpy operation, although it is a memory operation, it still consumes a little CPU. Today we tested the efficiency of executing memcpy in a single thread. The result is for configuring the work thread in TCP epoll.

The quantity has guiding significance. The following example shows how to execute memcpy quickly based on 8 K memory. One thread can copy 500 M in about 1 second. If the server bandwidth or network card reach the upper limit of 1 GB, the network I/O work thread can be opened with two threads. Considering the message parsing loss, the three threads are sufficient to resist the maximum Hardware load.

The test result is as follows:

Intel (R) Xeon (R) CPU E5405 @ 2.00 GHz

Do memcpy speed: 12.27 MS/MB
Each threads can do memcpy 667.645 MB

Copy codeThe Code is as follows: # include <iostream>
# Include <sys/time. h>
# Include <string. h>

Using namespace std;

Int main (int argc, char * argv [])
{
Long len = 8192;
Int loop = 200;
Char * p = new char [len];
Char * q = p;
Struct timeval start, end;
Gettimeofday (& start, NULL );
For (int I = 0; I <loop; ++ I)
{
Char * p = new char [len];
* P = char (I );
Memcpy (p, q, len );
Delete [] p;
}
Gettimeofday (& end, NULL );
Cout <"do memcpy speed:" <(end. TV _sec-start. TV _sec) * 1000 + double (end. TV _usec-start. TV _usec)/(len * loop/1000/1000)/loop <"MS/MB \ n ";
Cout <"each thread can do memcpy" <double (len) * loop/1000/1000/(end. TV _sec-start. TV _sec) + double (end. TV _usec-start. TV _usec)/1000/1000) <"MB \ n ";

}

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.