c vs golang(testing performance)

sumint_single_thread.c

/*************************************************************************
    > File Name: sumint.c
  > Author: perrynzhou
  > Mail: perrynzhou@gmail.com
  > Created Time: Sun 04 Mar 2018 02:51:53 AM EST
 ************************************************************************/

#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
typedef struct {
    uint64_t data;
    int count;
} value;
void add(value* v)
{
    for (int i = 0; i < v->count; i++) {
        __sync_fetch_and_add(&v->data, 1);
    }
}
int main(int argc, char* argv[])
{
    int num = atoi(argv[1]);
    pthread_t thds[num];
    value v = {
        .data = 0,
        .count = atoi(argv[2])
    };
    add(&v);
    fprintf(stdout, "running %d thread,&value{data=%ld,count=%d}\n", num, v.data, v.count);
    return 0;
}

sumint_multi_thread.c

#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
typedef struct {
    uint64_t data;
    int count;
} value;
void add(value* v)
{
    for (int i = 0; i < v->count; i++) {
        __sync_fetch_and_add(&v->data, 1);
    }
}
int main(int argc, char* argv[])
{
    int num = atoi(argv[1]);
    pthread_t thds[num];
    value v = {
        .data = 0,
        .count = atoi(argv[2])
    };
    for (int i = 0; i < num; i++) {
        pthread_create(&thds[i], NULL, (void*)&add, (void*)&v);
    }
    for (int i = 0; i < num; i++) {
        pthread_join(thds[i], NULL);
    }
    fprintf(stdout, "running %d thread,&value{data=%ld,count=%d}\n", num, v.data, v.count);
    return 0;
}

sumint.go

package main

import (
    "flag"
    "fmt"
    "sync"
    "sync/atomic"
)

func main() {
    count := flag.Int64("count", 10000, "count")
    thread := flag.Int("thread", 1, "thread")
    flag.Parse()
    var data uint64
    var wg sync.WaitGroup
    wg.Add(*thread)
    for i := 0; i < *thread; i++ {
        go func(d *uint64, wg *sync.WaitGroup) {
            var i int64
            defer wg.Done()
            for i = 0; i < *count; i++ {
                atomic.AddUint64(d, 1)
            }
        }(&data, &wg)
    }
    wg.Wait()
    fmt.Printf("running %d goroutine ,&Value{data=%v,count=%v}\n", *thread, data, *count)
}

test.sh

$ cat test.sh
#!/bin/bash
echo "-----c_sumint_single_test-----"
time ./c_sumint_single_test 1 20000000
echo "-----c_sumint_multi_test----"
time ./c_sumint_multi_test 20 1000000
echo "-----go_sumint_nulti_test----"
time ./go_sumint_test -thread=20 -count=1000000

性能測(cè)試結(jié)果


Jietu20180304-170107@2x.jpg

總結(jié)

1.linux 下pthread的性能開銷確實(shí)比golang 的goroutine開銷大,畢竟goroutine是running在pthread之上的,是在線程上實(shí)現(xiàn)的多并發(fā),上下文切換遠(yuǎn)比線程切換小。
2.linux下單線程的性能遠(yuǎn)比多個(gè)goroutine的性能好,少了很多的性能開銷。
3.在不同的場(chǎng)景下使用不同的programming tools解決不同的問題。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容