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:
- 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.
- 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