Go1.1效能測試報告(和C差距在10%以內)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

最近Go1.1正式發布, 根據官方的說法, Go1.1效能比Go1.0提升基本有30%-40%, 有時更多(當然也有不明顯的情況).

Go1.1的詳細介紹: Go1.1新特性介紹(語言和庫更完善/效能提高約30%).

這裡是針對Go1.1和C語言的效能測試: 測試的重點是語言的效能, 當然也會受到標準庫效能的影響.

測試環境

  • 測試程式: $GOROOT/test/bench/shootout/timing.sh
  • 硬體設定: Intel Core i7-3770 CPU @ 3.40GHz; 16GB記憶體
  • 作業系統: CentOS6.3 x86_64

補充: i7-3770是4核心8線程.

gcc和gc版本:

gcc -vgcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)go versiongo version go1.1 linux/amd64

測試結果

$GOROOT/test/bench/shootout/timing.shfasta -n 25000000    gcc -m64 -O2 fasta.c              0.86u 0.00s 0.87r    gc fasta                          0.85u 0.00s 0.86r    gc_B fasta                        0.83u 0.00s 0.83rreverse-complement < output-of-fasta-25000000    gcc -m64 -O2 reverse-complement.c 0.45u 0.05s 0.50r    gc reverse-complement             0.60u 0.05s 0.65r    gc_B reverse-complement           0.55u 0.04s 0.59rnbody -n 50000000    gcc -m64 -O2 nbody.c -lm          5.51u 0.00s 5.52r    gc nbody                          7.16u 0.00s 7.18r    gc_B nbody                        7.12u 0.00s 7.14rbinary-tree 15 # too slow to use 20    gcc -m64 -O2 binary-tree.c -lm    0.31u 0.00s 0.31r    gc binary-tree                    1.08u 0.00s 1.07r    gc binary-tree-freelist           0.15u 0.00s 0.15rfannkuch 12    gcc -m64 -O2 fannkuch.c           26.45u 0.00s 26.54r    gc fannkuch                       35.99u 0.00s 36.08r    gc fannkuch-parallel              73.40u 0.00s 18.58r    gc_B fannkuch                     25.18u 0.00s 25.25rregex-dna 100000    gcc -m64 -O2 regex-dna.c -lpcre   0.25u 0.00s 0.26r    gc regex-dna                      1.65u 0.00s 1.66r    gc regex-dna-parallel             1.72u 0.01s 0.67r    gc_B regex-dna                    1.64u 0.00s 1.65rspectral-norm 5500    gcc -m64 -O2 spectral-norm.c -lm  9.63u 0.00s 9.66r    gc spectral-norm                  9.63u 0.00s 9.66r    gc_B spectral-norm                9.63u 0.00s 9.66rk-nucleotide 1000000    gcc -O2 k-nucleotide.c -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -lglib-2.0  2.62u 0.00s 2.63r    gc k-nucleotide                   2.69u 0.01s 2.71r    gc k-nucleotide-parallel          3.02u 0.00s 0.97r    gc_B k-nucleotide                 2.66u 0.01s 2.68rmandelbrot 16000    gcc -m64 -O2 mandelbrot.c        20.95u 0.00s 21.01r    gc mandelbrot                    23.73u 0.00s 23.79r    gc_B mandelbrot                  23.72u 0.00s 23.79rmeteor 2098    gcc -m64 -O2 meteor-contest.c     0.05u 0.00s 0.05r    gc meteor-contest                 0.06u 0.00s 0.07r    gc_B meteor-contest               0.06u 0.00s 0.06rpidigits 10000    gcc -m64 -O2 pidigits.c -lgmp     0.77u 0.00s 0.77r    gc pidigits                       1.45u 0.01s 1.44r    gc_B pidigits                     1.45u 0.01s 1.43rthreadring 50000000    gcc -m64 -O2 threadring.c -lpthread     12.05u 261.20s 216.36r    gc threadring                           6.61u 0.00s 6.63rchameneos 6000000    gcc -m64 -O2 chameneosredux.c -lpthread 4.04u 21.08s 4.20r    gc chameneosredux                       4.97u 0.00s 4.99r

測試結果說明

其中gc_B是開了-B選項, 選項的說明如下:

go tool 6g -husage: 6g [options] file.go...  -+    compiling runtime  -%    debug non-static initializers  -A    for bootstrapping, allow 'any' type  -B    disable bounds checking...

應該就是禁用了Go的slice下標越界檢測等特性.

測試的結果顯示Go的效能已經和C語言已經非常接近了, 有極個別的情境甚至比C還好(binary-tree).

根據$GOROOT/test/bench/shootout/timing.log的資料, gccgo 的最佳化應該更好一點.

不過目前gccgo的標準庫比gc標準庫可能要差一些(gccgo1.1還未發布), 因此有些測試效能比gc差一些.

我電腦沒有安裝gccgo, 因此只有gcc/gc/gc_B三個測試結果.

關於 BenchmarksGame 的測試差異

http://benchmarksgame.alioth.debian.org/u64q/go.php

說明: BenchmarksGame
不是為了測試不同語言的最優效能, 而且為了測試不同語言最自然狀態下編寫程式的效能.
比如, binary-trees測試就是禁止自己定製專有的緩衝池的, 而C語言的版本則可以隨意使用
各種最佳化手段(基於apr和緩衝池和openmp的並行最佳化).

BenchmarksGame的測試結果中, 有幾個Go的效能很差(已經提交了spectral-norm的最佳化版本):

Benchmark      Time Memory   Codefasta           3×    3×      ±spectral-norm   4×    3×      ±binary-trees   13×    4×      ±regex-dna †    26×    ±      1/4

補充: BenchmarksGame 測試的是程式實際已耗用時間, GoBench 是測試的CPU時間. 如果 GoBench 要減少實際時間, 需要充分利用多個CPU的運算資源(Go測試代碼對多CPU的效能還有待進一步分析).

補充: BenchmarksGame的
binary-trees測試禁止自己定製專有的緩衝池. 因此, 只能寄希望於Go的GC效能改進或新的sync.Cache能儘快發布.

為了方便比較, 在同一台機器上對比了 BenchmarksGame 和 GoBench 的測試結果 .

完整測試代碼在: https://bitbucket.org/chai2010/gobench

spectral-norm.go的最佳化

Go內建的spectral-norm測試效能比BenchmarksGame的效能要低.

我對spectral-norm的代碼進行了最佳化, 最佳化之後的效能比BenchmarksGame要好一些.

最佳化前的代碼(spectral-norm.go):

func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }v[i] += evalA(i, j) * u[j]

函數evalA將整數運算式先轉換為float64, 然後做了一次倒數運算. 其中倒數的運算有一定冗餘.

最佳化後的代碼(chai2010-spectral-norm.go):

func evalA(i, j int) int { return ((i+j)*(i+j+1)/2 + i + 1) }v[i] += u[j] / float64(evalA(i, j))

只在必要的時候才做浮點的轉換, 減少了一次倒數的運算.

測試時間由 9.62u 減少到 4.35u, 效能提高約1倍.

完整的對比結果如下:

./timing.shfasta -n 25000000    gcc -m64 -O3 -fomit-frame-pointer -march=native -mfpmath=sse -msse3 alioth-fasta.gcc-2.c   0.96u 0.18s 1.15r    gcc -m64 -O3 alioth-fasta.gcc-2.c       0.92u 0.22s 1.15r    gcc -m64 -O2 alioth-fasta.gcc-2.c       0.99u 0.15s 1.15r    gc alioth-fasta 3.04u 0.00s 3.06r    gc_B alioth-fasta       3.03u 0.00s 3.04r    gcc -m64 -O3 fasta.c    0.87u 0.00s 0.87r    gcc -m64 -O2 fasta.c    0.86u 0.00s 0.87r    gc fasta        0.85u 0.00s 0.86r    gc_B fasta      0.83u 0.00s 0.83rspectral-norm 5500    g++ -m64 -O3 -march=native -fopenmp -mfpmath=sse -msse2 alioth-spectralnorm.gpp-2.cpp   4.69u 0.00s 0.59r    g++ -m64 -O2 -march=native -fopenmp -mfpmath=sse -msse2 alioth-spectralnorm.gpp-2.cpp   4.69u 0.00s 0.59r    gc alioth-spectralnorm  6.75u 0.00s 1.67r    gc_B alioth-spectralnorm        6.93u 0.00s 1.69r    gcc -m64 -O2 spectral-norm.c -lm        9.63u 0.00s 9.66r    gc spectral-norm        9.62u 0.00s 9.65r    gc_B spectral-norm      9.62u 0.00s 9.66r    gc spectral-norm-parallel       9.86u 0.00s 4.91r    gc_B spectral-norm-parallel     9.85u 0.00s 4.90r    gc chai2010-spectral-norm       4.35u 0.00s 4.36r    gc_B chai2010-spectral-norm     4.35u 0.00s 4.36r    gc chai2010-spectral-norm-parallel      5.15u 0.00s 2.21r    gc_B chai2010-spectral-norm-parallel    4.58u 0.00s 2.22rbinary-tree 15 # too slow to use 20    gcc -m64 -O3 -fomit-frame-pointer -march=native -fopenmp -I/usr/include/apr-1 -lapr-1 -lgomp alioth-binarytrees.gcc-7.c -lm 0.21u 0.00s 0.03r    gcc -m64 -O3 -fopenmp -I/usr/include/apr-1 -lapr-1 -lgomp alioth-binarytrees.gcc-7.c -lm   0.21u 0.00s 0.03r    gcc -m64 -O2 -fopenmp -I/usr/include/apr-1 -lapr-1 -lgomp alioth-binarytrees.gcc-7.c -lm   0.23u 0.00s 0.03r    gc alioth-binarytrees   2.16u 0.04s 0.42r    gc_B alioth-binarytrees 2.20u 0.04s 0.42r    gcc -m64 -O3 binary-tree.c -lm  0.34u 0.00s 0.34r    gcc -m64 -O2 binary-tree.c -lm  0.31u 0.00s 0.31r    gc binary-tree  1.07u 0.00s 1.07r    gc binary-tree-freelist 0.15u 0.00s 0.15rregex-dna 100000    gc alioth-regexdna      2.07u 0.00s 0.64r    gc_B alioth-regexdna    2.22u 0.00s 0.71r    gcc -m64 -O2 regex-dna.c -lpcre 0.25u 0.00s 0.26r    gc regex-dna    1.63u 0.00s 1.64r    gc regex-dna-parallel   1.84u 0.00s 0.72r    gc_B regex-dna  1.63u 0.00s 1.64r

分析測試的資料, 有以下幾個特徵(僅針對當前的測試):

  • gcc的-O3和-O2以及-fomit-frame-pointer等最佳化參數對效能的最佳化在20%以內, 可以暫時忽略
  • gc_B和gc的對效能的最佳化在5%以內, 可以暫時忽略
  • BenchmarksGame 和 GoBench 的C版本測試程式效能很接近(fasta是BenchmarksGame稍好, binary-tree是GoBench稍好)
  • 大部分BenchmarksGame 和 GoBench 的Go版本測試程式效能差距巨大, GoBench要比BenchmarksGame快2~4X.
  • BenchmarksGame 的 spectralnorm 測試效能比 GoBench 的要好(6.75u/9.86u)
  • 經過我手工最佳化過的 GoBench 的 chai2010-spectral-norm 測試效能比 BenchmarksGame 的要好(5.15/6.75u)
  • BenchmarksGame 的 regexdna 暫時缺少C語言版本的測試資料(沒有編譯過)
  • GoBench 的 binary-tree-freelist 使用記憶體池對效能提高約 7X.

綜上可以發現 BenchmarksGame 的很多 Go 版本測試程式效能較差(甚至快到10X的差距).
關於 BenchmarksGame 的 Go 測試程式效能低的原因, 大家可以自己去分析代碼.

關於regex的測試主要是因為Go的regex標準庫比高度最佳化的C庫pcre還是較慢,
目前Go的regex庫還有待進一步的最佳化.

關於pidigits的測試, BenchmarksGame 和 GoBench 基本是一致的:
目前math/big和gmp的效能還有2倍的差距.

關於BenchmarksGame和GoBench 的測試差異的很多細節還需要進一步分析.
重點應該是64位系統下多核和並發的效能對比(畢竟Go是多核時代的程式設計語言).

官方的測試結論

http://go.googlecode.com/hg/test/bench/shootout/timing.log:

# Sep 26, 2012# 64-bit ints, plus significantly better floating-point code.# Interesting details:#   Generally something in the 0-10% slower range, some (binary tree) more#   Floating-point noticeably faster:#       nbody -25%#       mandelbrot -37% relative to Go 1.#   Other:#       regex-dna +47%

Go已經和C差距在10%以內, 有特殊情境效能甚至更好.

2013.11.26 補充的官方資料:

# May 23, 2013# Go 1.1, which includes precise GC, new scheduler, faster maps.# 20%-ish speedups across many benchmarks.# gccgo showing significant improvement (even though it's not yet up to Go 1.1)## Standouts:#   fannkuch, regex-dna, k-nucleotide, threadring, chameneos

在很多測試中, gccgo的效能已經開始超越gc編譯的程式的效能.

大數運算的效能比 gmp 還在差 30~40%.

正則比pcre還慢約 5 倍.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.