This is a creation in Article, where the information may have evolved or changed.
Simply test the switching efficiency of Go (Golang) and Libtask, libtask a C-language co-Libraries, is the predecessor of the Go language early prototype,
Test machine is my Mac Air installed CentOS virtual machine (only one core)
The code does not take any optimizations, just use the default configuration
Test conclusion:
Golang switching 100w times requires 295ms
Libtask switching 100w times requires 1446ms
Package Mainimport ( "FMT" "Runtime" "Time " ) var i int = 0func 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 goroutinetest () TV2: = time. Now (). Unixnano () Usetime: = (TV2-TV1)/1000000fmt. Printf ("Use time: %DMS \ n", Usetime) fmt. PRINTF ("End main \ n")}
lib task test code:
#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;stru CT Timeval TV1, tv2;voidtestswitch (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);}} Voidtaskmain (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);}
copyright notice: Reprint Please specify the source, thank you Http://blog.csdn.net/mumumuwudi