Go language pass-through and pointer performance comparison

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

Reprinted from Dada's blog


Previous C programming experience has taught us that pointers are better for performance, and this experience is common in the go language. But there are two things to keep in mind:

    1. The pointer to the participants caused the operation of the domain is not sure, in the end the function will not change the incoming object? There is no const keyword like type C in the go language, so it cannot be controlled.
    2. The go language has a GC, and this GC is not very perfect, affected by the number of objects, passing pointers means that it is possible to create more unnecessary objects, in the end, the performance of the pointer-to-pass can not offset the creation of the object generated by the GC pressure? This is a poorly observed data. (the original author Dakota Here should be wrong, the first pass value is also copied, to produce a new object, the allocation of memory address to the new function, the second pointer is generally 64 bytes, the cost of copying far smaller large and medium-sized data transfer value, so in the case of a new object is generated, Pointer still takes precedence over medium-large value structure)

The following is the test code for performance consumption:

 PackageLabs02Import "Testing"typeBigstructstruct{C01UInt64C02UInt64C03UInt64C04UInt64C05UInt64C06UInt64C07UInt64C08UInt64C09UInt64C10UInt64C11UInt64C12UInt64C13UInt64C14UInt64C15UInt64C16UInt64C17UInt64C18UInt64C19UInt64C20UInt64C21UInt64C22UInt64C23UInt64C24UInt64C25UInt64C26UInt64C27UInt64C28UInt64C29UInt64C30UInt64}funcInvoke1 (a *bigstruct)UInt64{returnA.C30}funcInvoke2 (a bigstruct)UInt64{returnA.C30}funcBenchmark_invoke1 (b *testing. B) {varA =New(bigstruct) forI: =0; i < B.N; i++ {Invoke1 (a)}}funcBenchmark_invoke2 (b *testing. B) {varA = bigstruct{} forI: =0; i < B.N; i++ {Invoke2 (a)}}

Test the efficiency of the value and pointer parameters.

Experimental results:

Dada-imac:misc dada$ Go test-test.bench= "." Labs02testing:warning:no tests to Runpassbenchmark_invoke1    2000000000          0.52 ns/opbenchmark_invoke2    100000000           12.8 ns/opok      labs02  2.403s

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.