Testing the interface efficiency of the go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Interface is a major feature of the go language, not even the soul.

Interface should appear and be used extensively in go programs because it is necessary to understand and test its efficiency.


Test ideas:

Using a vector bag, test the efficiency of the native intvector and the vector that was packaged with interface.

The vector bag was removed from the Go1, but I kept a copy of the vector code,

It should also be found in the code base. I found a version of: Https://code.google.com/p/go/source/browse?name=weekly.2011-08-17#hg%2Fsrc%2Fpkg%2Fcontainer%2Fvector

may be different from my test code, not carefully compared, but should be similar.

Implementation of interface in the Go language: http://research.swtch.com/interfaces

According to the author, a function call takes 5 CPU instructions (the virtual function of C + + is 3).

Here is the test code:

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 ()}

Test results:

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.

Apparently native Intvector are 2 to 3 times times faster than vectors packaged with interface.


Summarize:

The interface in the go language is flexible, but it also pays a certain price for performance.

If it is a performance-critical code, consider abandoning interface and writing your native code yourself.

In other words, there is no generic mechanism, really compared to the egg pain, quite expect go to support generics.

For generics in Go, see: The Dilemma of generic programming



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.