在進行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";
}