Golang a beginner's test of a function return value or a pointer

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

First put the simple code out, and then nonsense.

First benchmark's orders.

go test -bench=Poin -memprofile=pointermem.pprof -memprofilerate=1 -benchmem
go test -bench=Value -memprofile=valuemem.pprof -memprofilerate=1 -benchmem

Then is the result of pprof, first pointer, then value, you can see the pointer report 2allocs/op

package maintype mytype struct {field1 stringfield2 stringfield3 stringfield4 stringfield5 stringfield6 stringfield7 stringfield8 stringfield9 string}func returnValue() mytype {return mytype{field1: "abc",field2: "aaa",field3: "vvv",field4: "ddd",field5: "394834",field6: "sdfbd34534",field7: "asdf34534d90sdfsd",field8: "asdf3424jjiiihiu",field9: "vmxvbxcvb8rewasdfasdf",}}func returnPointer() *mytype {return &mytype{field1: "abc",field2: "aaa",field3: "vvv",field4: "ddd",field5: "394834",field6: "sdfbd34534",field7: "asdf34534d90sdfsd",field8: "asdf3424jjiiihiu",field9: "vmxvbxcvb8rewasdfasdf",}}func main() {}
package mainimport ("fmt""testing")func BenchmarkReturnValue(b *testing.B) {var x mytypefor i := 0; i < b.N; i++ {x = returnValue()fmt.Println(x)}}func BenchmarkReturnPointer(b *testing.B) {var x *mytypefor i := 0; i < b.N; i++ {x = returnPointer()fmt.Println(*x)}}

Related Article

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.