Attached two files main.c and main.go, both try to compute fib(40), 20 times on `x` no.of threads/goroutines. fib(40) is recursive.
*Prerequisites for C:* 1. libuv (https://github.com/libuv/libuv) *Compile*: - *C :* - *Unoptimized:* * gcc main.c -lpthread -luv * - *Optimized:* gcc-O3 main.c -lpthread -luv - *Go :* - go build *Run:* - *C:time UV_THREADPOOL_SIZE=x ./a.out // substitute `x` with physical core count * - *GO:./temp x // substitute `x` with physical core count * Results: Thread CountC (Optimized)C (Unoptimized)GO 1 4.38s 11.36s 11.7s 4 1.12s 3.1s 2.9s 8 1.1s 2.9s 2.7s Laptop with 4 physical cores(8 logical cores). Why can't go provide Optimization flag? I understand go developers want compiler to be simple, but the difference seems too big to leave it out. But isn't there any possibility to abstract the complexity and provide optimization flag? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/cec68183-27d1-47cc-b106-48766a2242e6%40googlegroups.com.
#include <stdio.h> #include <uv.h> uv_loop_t* loop; int _fib(int n) { if (n < 2) { return 1; } return _fib(n - 2) + _fib(n - 1); } void fib(uv_work_t* req) { int n = *(int*)req->data; // get n ==> 40 fprintf(stdout, "fib(%d)=%d\n", n, _fib(n)); } // compute fib(40), 20 times int main() { loop = uv_default_loop(); int data[20]; uv_work_t req[20]; for (int i = 0; i < 20; i++) { data[i] = 40; // set fib num to compute, (40) req[i].data = (void*)&data[i]; // queue work, to run in multiple threads uv_queue_work(loop, &req[i], fib, NULL); } // execute return uv_run(loop, UV_RUN_DEFAULT); }
main.go
Description: Binary data