asm 比 ucontext 快
測試原裝libco的asm版 ?與 ?libco的ucontext版性能, 同一個機器、同一份代碼, 連接不同的libco實現庫。 ?asm版比ucontext快6倍左右。
但是有網友說:
但是1秒切換100萬次的應用不多,更多消耗在共享棧拷貝
更多消耗在設置高低電平
libco這種只能是半雙工一應一答式, 用udp, 短連接tcp, 或者長連接tcp半雙工模式。
長連接的全雙工不行, fastrpc底層的協程庫是長連接全雙工模式。
半雙工時, 多開連接數可以提高性能,但也不是越多越好。
#include "co_routine.h"
#include <sys/time.h>#include <stdio.h>
struct Ctx
{
? ? stCoRoutine_t *pCo;
};
int cocount = 2;
int testcount = 1000000;
Ctx *arrayTask = NULL;
int testi = 0;
#define LOGD(x, ...) (x, ##__VA_ARGS__)
#define LOGI(x, ...) printf(x, ##__VA_ARGS__)
void *func1(void *p)
{
? ? co_enable_hook_sys();
? ? while(1)
? ? {
? ? ? ? LOGD("func1 %d start\n", testi);
? ? ? ? co_yield_ct();
? ? ? ? LOGD("func1 %d end\n", testi);
? ? ? ? if(testi == testcount) break;
? ? }
? ? LOGD("func1 end...\n");
}
void *func0(void *p)
{
? ? timeval beg,end;
? ? co_enable_hook_sys();
? ? LOGD("func0 poll start\n");
? ? poll(0,0,1000);
? ??LOGD("func0 poll end\n");
? ? gettimeofday(&beg, NULL);
? ? for(; testi < testcount; testi++)
? ? {
? ? ? ? LOGD("func0 %d start\n", testi);
? ? ? ? co_resume(arrayTask[1].pCo);
? ? ? ? LOGD("func0 %d end\n", testi);
? ? }
? ? co_resume(arrayTask[1].pCo);
? ? gettimeofday(&end, NULL);
? ? LOGI("switch %d times, cost: %d(us)\n", testcount, 1000000*(end.tv_sec-beg.tv_sec) + end.tv_usec-beg.tv_usec);
? ? LOGD("func0 end...\n");
}
void run()
{
? ? arrayTask = new Ctx[cocount];
// ? ?co_enable_hook_sys();
? ? co_create(&arrayTask[0].pCo, NULL, func0, &arrayTask[0]);
? ? co_resume(arrayTask[0].pCo);
? ? co_create(&arrayTask[1].pCo, NULL, func1, &arrayTask[1]);
? ? co_resume(arrayTask[1].pCo);
? ? co_eventloop(co_get_epoll_ct(), 0, 0);
? ? LOGD("end of run...\n");
}
int main(int, char *[])
{
? ? run();
? ? return 0;
}
總結
以上是生活随笔為你收集整理的asm 比 ucontext 快的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos 上假设svnserve
- 下一篇: go 的des加解密