Simply test the switching efficiency of the Go (Golang) and libtask processes

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.