C ++ executes the memory memcpy Efficiency Test

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

 

 1 # Include <iostream>
2 # Include <sys/time. h>
3 # Include < String . H>
4
5 Using Namespace STD;
6
7 Int Main ( Int Argc, Char * Argv [])
8 {
9 Long Len = 8192 ;
10 Int Loop = 200 ;
11 Char * P = New Char [Len];
12 Char * Q = P;
13 Struct Timeval start, end;
14 Gettimeofday (& START, null );
15 For (Int I = 0 ; I <loop; ++ I)
16 {
17 Char * P = New Char [Len];
18 * P = Char (I );
19 Memcpy (p, q, Len );
20 Delete [] P;
21 }
22 Gettimeofday (& End, null );
23 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 " ;
24 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 " ;
25
26 }

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.