分享
简单测试一下go(golang)和libtask协程的切换效率
mumumuwudi · · 4605 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
简单测试一下go(golang)和libtask协程的切换效率, libtask一个C语言的协程库,是go语言的前身很早期的原型,
测试机器是我的mac air 安装的centos虚拟机(只有一个核)
代码没有采用任何优化,只是使用默认配置
测试结论:
golang 切换100w次 需要 295ms
libtask 切换100w次 需要1446ms
package main
import (
"fmt"
"runtime"
"time"
)
var i int = 0
func test(){
for{
i++
if i >1000000{
fmt.Printf("end switch \n")
break
}else{
//fmt.Printf(" in test i:%d \n", i)
}
runtime.Gosched()
}
}
func main(){
fmt.Printf("GOMAXPROCS :%d \n",runtime.GOMAXPROCS(1))
tv1 := time.Now().UnixNano()
go test()
// in main goroutine
test()
tv2 := time.Now().UnixNano()
usetime := (tv2 - tv1)/1000000
fmt.Printf("use time: %dms \n", usetime)
fmt.Printf("end main \n")
}lib task测试代码:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <task.h>
#include<sys/time.h>
extern int tasknswitch;
enum { STACK = 32768 };
int i = 0;
struct timeval tv1, tv2;
void
testswitch(void *v)
{
int ms;
while(1)
{
i++;
if( i< 1000000)
{
//printf("int task i:%d \n", i);
taskyield();
}
else
{
// chansendul(c, 0);
//taskexitall(0);
gettimeofday(&tv2,NULL);
ms = ((tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec))/1000;
printf("time use %d\n ", ms);
printf("task swithch %d \n", tasknswitch);
taskexitall(0);
}
}
}
void
taskmain(int argc, char **argv)
{
int n;
//c = chancreate(sizeof(unsigned long), 0);
gettimeofday(&tv1, NULL);
//for(i=1; i<; i++){
taskcreate(testswitch, 0, STACK);
//}
testswitch(NULL);
}版权声明:转载请注明出处, 多谢 http://blog.csdn.net/mumumuwudi
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信4605 次点击
下一篇:GoLang 开发笔记
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
简单测试一下go(golang)和libtask协程的切换效率, libtask一个C语言的协程库,是go语言的前身很早期的原型,
测试机器是我的mac air 安装的centos虚拟机(只有一个核)
代码没有采用任何优化,只是使用默认配置
测试结论:
golang 切换100w次 需要 295ms
libtask 切换100w次 需要1446ms
package main
import (
"fmt"
"runtime"
"time"
)
var i int = 0
func test(){
for{
i++
if i >1000000{
fmt.Printf("end switch \n")
break
}else{
//fmt.Printf(" in test i:%d \n", i)
}
runtime.Gosched()
}
}
func main(){
fmt.Printf("GOMAXPROCS :%d \n",runtime.GOMAXPROCS(1))
tv1 := time.Now().UnixNano()
go test()
// in main goroutine
test()
tv2 := time.Now().UnixNano()
usetime := (tv2 - tv1)/1000000
fmt.Printf("use time: %dms \n", usetime)
fmt.Printf("end main \n")
}lib task测试代码:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <task.h>
#include<sys/time.h>
extern int tasknswitch;
enum { STACK = 32768 };
int i = 0;
struct timeval tv1, tv2;
void
testswitch(void *v)
{
int ms;
while(1)
{
i++;
if( i< 1000000)
{
//printf("int task i:%d \n", i);
taskyield();
}
else
{
// chansendul(c, 0);
//taskexitall(0);
gettimeofday(&tv2,NULL);
ms = ((tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec))/1000;
printf("time use %d\n ", ms);
printf("task swithch %d \n", tasknswitch);
taskexitall(0);
}
}
}
void
taskmain(int argc, char **argv)
{
int n;
//c = chancreate(sizeof(unsigned long), 0);
gettimeofday(&tv1, NULL);
//for(i=1; i<; i++){
taskcreate(testswitch, 0, STACK);
//}
testswitch(NULL);
}版权声明:转载请注明出处, 多谢 http://blog.csdn.net/mumumuwudi