2017-08-01 2 views
-2

私はGoを学び始めました、楽しく簡単です。しかし、私はgoroutinesを使って作業しても、パフォーマンスにほとんど影響しませんでした。Goroutineのパフォーマンス

私は順次2つの機能では100万個の数字2回追加しようとすると:それは5ミリ秒かかり

package main 

import (
    "fmt" 
    "time" 
) 

var sumA int 
var sumB int 

func fSumA() { 
    for i := 0; i < 1000000; i++ { 
     sumA += i 
    } 
} 

func fSumB() { 
    for i := 0; i < 1000000; i++ { 
     sumB += i 
    } 
} 

func main() { 
    start := time.Now() 
    fSumA() 
    fSumB() 
    sum := sumA + sumB 
    fmt.Println("Elapsed time", time.Since(start)) 
    fmt.Println("Sum", sum) 
} 

を。

MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.724406ms 
Suma total 999999000000 
MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.358165ms 
Suma total 999999000000 
MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.042528ms 
Suma total 999999000000 
MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.469628ms 
Suma total 999999000000 

私は2つのゴルーチンで同じことをやろう

package main 

import (
    "fmt" 
    "sync" 
    "time" 
) 

var wg sync.WaitGroup 

var sumA int 
var sumB int 

func fSumA() { 
    for i := 0; i < 1000000; i++ { 
     sumA += i 
    } 
    wg.Done() 
} 

func fSumB() { 
    for i := 0; i < 1000000; i++ { 
     sumB += i 
    } 
    wg.Done() 
} 

func main() { 
    start := time.Now() 
    wg.Add(2) 
    go fSumA() 
    go fSumB() 
    wg.Wait() 
    sum := sumA + sumB 
    fmt.Println("Elapsed time", time.Since(start)) 
    fmt.Println("Sum", sum) 
} 

私は、5ミリ秒、多かれ少なかれ、同じ結果を得ることができます。私のコンピュータはMacBook Pro(Core 2 Duo)です。パフォーマンスの改善は見られません。多分プロセッサーですか?あなたはgolangs独自のベンチマークツールでこれをテストする方法をここで

MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.258415ms 
Suma total 999999000000 
MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.528498ms 
Suma total 999999000000 
MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.273565ms 
Suma total 999999000000 
MacBook-Pro-de-Pedro:hello pedro$ ./bin/hello 
Elapsed time 5.539224ms 
Suma total 999999000000 
+5

私はあなたのCPUがどれくらい速く1百万になるかをはるかに過大評価していると思います。ミリ秒単位で表示するには十分な作業ではありません。 5msはほぼすべてのプロセスの起動であり、コンソールに出力します。 – captncraig

+0

また、実行しているバージョンは何ですか。 'go version'? – captncraig

+1

このようなベンチマークを行う場合は、「go test」ベンチマークを使用します。少なくとも合理的な入力番号を得て、多くの実行結果を平均します。 – JimB

答えて

2

をテスト行くのファイル(例えばmain_test.go)を作成します。

注:_test.goはファイルの末尾にする必要があります。

次のコードをコピーしあったり作成し、独自のベンチマーク:

package main 

import (
    "sync" 
    "testing" 
) 

var GlobalInt int 

func BenchmarkCount(b *testing.B) { 
    var a, c int 
    count(&a, b.N) 
    count(&c, b.N) 
    GlobalInt = a + c // make sure the result is actually used 
} 

func count(a *int, max int) { 
    for i := 0; i < max; i++ { 
     *a += i 
    } 
} 

var wg sync.WaitGroup 

func BenchmarkCountConcurrent(b *testing.B) { 
    var a, c int 
    wg.Add(2) 
    go countCon(&a, b.N) 
    go countCon(&c, b.N) 
    wg.Wait() 

    GlobalInt = a + c // make sure the result is actually used 
} 

func countCon(a *int, max int) { 
    for i := 0; i < max; i++ { 
     *a += i 
    } 
    wg.Done() 
} 

ランで:私のMac上の

go test -bench . 

結果:

$ go test -bench . 
BenchmarkCount-8    500000000   3.50 ns/op 
BenchmarkCountConcurrent-8  2000000000   1.98 ns/op 
PASS 
ok  MyPath/MyPackage  6.309s 

ほとんどの重要な価値があります時間/操作。より小さいほど良い。ここでは、通常のカウントで3.5ns/op、同時計数で1.98ns/op。

編集: ここではゴランTesting and Benchmarkを読むことができます。

+0

@ペドロ:あなたは私の答えに何かを見逃していますか?そうでない場合は、正しいとマークできますか? – TehSphinX

関連する問題