Analysis of memcpy efficiency test based on C++

  • 2020-04-01 21:39:19
  • OfStack

When performing a memcpy operation, which is a memory operation but still USES a little bit of CPU, the efficiency of executing memcpy in a single thread was tested today, which results in configuring a work thread in TCP epoll

Quantity is instructive. As follows, the memory based on 8K is fast to execute memcpy, one thread can copy 500M about 1S, if the server bandwidth or network card to the upper limit is 1G, then the network IO work thread can open 2, considering the message parsing loss, 3 threads is enough to withstand the maximum load of the hardware.

After I go to the test machine, the test result is:

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

Do memcpy speed: 12.27 ms/MB
Each thread can do memcpy 667.645 MB


#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/MBn";
         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) <<" MBn";

 }


Related articles: