測試Go語言的interface的效率

來源:互聯網
上載者:User

interface是Go語言中的一大特點,甚至說是靈魂也不為過。

interface應該會在Go程式中大量出現和使用,因為有必要瞭解和測試下它的效率。

測試思路:

使用vector包,測試原生的IntVector和用interface封裝後的vector的效率。

Go1中去掉了vector包,不過當時我把vector的代碼保留了一份,

在程式碼程式庫裡應該也能找到。我找到了一個版本的:https://code.google.com/p/go/source/browse?name=weekly.2011-08-17#hg%2Fsrc%2Fpkg%2Fcontainer%2Fvector

可能和我測試用的代碼有差別,沒仔細對比過,不過應該差不多。

Go語言中interface的實現:http://research.swtch.com/interfaces

據作者說一次函數調用要花5條CPU指令(C++的虛函數是3條)。

下面是測試代碼:

package mainimport ("fmt""time". "vector"//"vector/vector")const size = 1000000func testIntVectorPush() {v := make(IntVector, size)t0 := time.Now()for i := 1; i < size; i++ {v.Push(i)}t1 := time.Now()fmt.Printf("The testIntVectorPush call took %v to run.\n", t1.Sub(t0))v = nil}func testIntVectorAt() {v := make(IntVector, size)t0 := time.Now()for j := 0; j < 1000; j++ {for i := 1; i < size; i++ {v.At(i)}}t1 := time.Now()fmt.Printf("The testIntVectorAt call took %v to run.\n", t1.Sub(t0))v = nil}func testVectorPush() {v := make(Vector, size)t0 := time.Now()for i := 1; i < size; i++ {v.Push(i)}t1 := time.Now()fmt.Printf("The testVectorPush call took %v to run.\n", t1.Sub(t0))v = nil}func testVectorAt() {v := make(Vector, size)t0 := time.Now()for j := 0; j < 1000; j++ {for i := 1; i < size; i++ {v.At(i)}}t1 := time.Now()fmt.Printf("The testVectorAt call took %v to run.\n", t1.Sub(t0))v = nil}func main() {fmt.Println("abc")i := 0for ; i < size; i++ {i += i}fmt.Println(i)testIntVectorPush()testIntVectorPush()testVectorPush()testVectorPush()testIntVectorAt()testIntVectorAt()testVectorAt()testVectorAt()}

測試結果:

The testIntVectorPush call took 19.0011ms to run.
The testIntVectorPush call took 19.0011ms to run.
The testVectorPush call took 64.0037ms to run.
The testVectorPush call took 51.0029ms to run.
The testIntVectorAt call took 1.2920739s to run.
The testIntVectorAt call took 1.2990743s to run.
The testVectorAt call took 2.4831421s to run.
The testVectorAt call took 2.5131438s to run.

明顯地原生的IntVector比用interface封裝過的Vector要快2到3倍。

總結:

Go語言中的interface很靈活,但是也付出了一定的效能代價。

如果是效能關鍵的代碼,可以考慮放棄interface,自己寫原生的代碼。

話說回來,沒有泛型機制,真的比較蛋疼,相當期待Go能支援泛型。

關於Go中的泛型,參見:泛型程式設計的困境

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.