基於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

複製代碼 代碼如下:#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";

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.