C++執行記憶體memcpy的效率測試

來源:互聯網
上載者:User

 

在進行memcpy操作時,雖然是記憶體操作,但是仍然是耗一點點CPU的,今天測試了一下單線程中執行memcpy的效率,這個結果對於配置TCP epoll中的work thread

數量有指導意義。如下基於8K的記憶體快執行memcpy, 1個線程大約1S能夠拷貝500M,如果伺服器頻寬或網卡到上限是1G,那麼網路io的work thread 開2個即可,考慮到訊息的解析損耗,3個線程足以抗住硬體的最高負載。

在我到測試機器上到測試結果是:

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

do memcpy speed:12.27 ms/MB
each thread 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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.