Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the details of golang efficient coding

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what are the details of efficient coding in golang". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn what are the details of efficient coding in golang.

Xdm, we all know that golang is an inherently highly concurrency, efficient compiled language.

But we all know that no matter how good the tools are, they are all in vain. Let's give two common paths to feel it.

Who do struct and map use?

When the amount of calculation is very small, the time-consuming gap between using temporary struct and map may not be seen, but when the number increases, the gap becomes obvious, and the greater the number, the more obvious the gap.

When we encounter that both keys and values can be fixed, we choose struct much more efficiently than map.

We simulated 100 million cycles to see how long it would take to use their respective data structures.

Calculate the current time before the loop

Calculate the current time after the loop

Finally, the difference between the two times is calculated, here we use milliseconds as units.

Func main () {T1: = time.Now (). UnixNano () / 1e6 for I: = 0; I < 1000000000; iTunes + {var test struct {Name string hobby string} test.Name = "xiaomotong" test.hobby = "program"} T2: = time.Now (). UnixNano () / 1e6 fmt.Println ("t2-t1 = =", t2-t1)}

The program runs to see the effect:

# go run main.go

T1 = = 1634377149185

T2 = = 1634377149221

T2-T1 = = 36

It takes 36 ms to use struct. How do you feel about this time?

Let's take a look at how to use map.

Func main () {T1: = time.Now (). UnixNano () / 1e6 fmt.Println ("T1 = =", T1) for I: = 0; I < 1000000000; iTunes + {var test = map [string] interface {} test ["name"] = "xiaomotong" test ["hobby"] = "program"} T2: = time.Now (). UnixNano () / 1e6 fmt.Println ("T2 =", T2) fmt.Println ("t2-t1 = =", t2-t1)}

The program runs to see the effect:

# go run main.go

T1 = = 1634377365927

T2 = = 1634377373525

T2-T1 = = 7598

Using struct takes 7598 ms

Using map and struct to complete the same data processing, the time difference is 212x. Which data structure would you choose for the above scenarios when we code?

Why is the above gap so big? the reason is that

In cases where we can determine the fields, we do not need to assign content dynamically at run time using a temporary Struct

But map is different. Map has to check the index, which is very time-consuming.

How to concatenate strings?

When coding xdm encounters string concatenation at work, how is it realized? Our tools are temporarily available as follows:

Use the method of +

How to use fmt.Sprintf ()

The way you use strings.Join

The way you use buffer

Seeing here, maybe we all have our own answers, but let's do it again to see how time-consuming their respective processing is in the case of the same string concatenation.

In the form of +

Let's calculate the loop and append the string 500000 times to see how much time it takes.

Func main () {T1: = time.Now (). UnixNano () / 1e6 fmt.Println ("T1 = =", T1) s: = "xiao" for I: = 0; I < 500000; iTunes + {s + = "motong"} T2: = time.Now () .UnixNano () / 1e6 fmt.Println ("T2 = =", T2) fmt.Println ("t2-t1 = =", t2-t1)}

The program runs to see the effect:

# go run main.go

T1 = = 1634378595642

T2 = = 1634378743119

T2-T1 = = 147477

Xdm was shocked to see this data. It was so slow. It took 147477 ms. That's a proper 2 minutes and 27 seconds.

Using + to process strings in Go language consumes a lot of performance, as we can see from the data.

How to use fmt.Sprintf ()

Func main () {T1: = time.Now (). UnixNano () / 1e6 fmt.Println ("T1 = =", T1) s: = "xiao" for I: = 0; I < 500000; iTunes + {s = fmt.Sprintf ("% s% s", s, "motong")} T2: = time.Now (). UnixNano () / 1e6 fmt.Println ("T2 =", T2) fmt.Println ("t2-t1 = =", t2-t1)}

The program runs to see the effect:

# go run main.go

T1 = = 1634378977361

T2 = = 1634379240292

T2-T1 = = 262931

Seeing this data, we were shocked that it took 262931 ms, a total of 4 minutes and 22 seconds. Did xdm not expect that using fmt.Sprintf is slower than using +?

The way you use strings.Join

Func main () {T1: = time.Now (). UnixNano () / 1e6 fmt.Println ("T1 = =", T1) s: = "xiao" for I: = 0; I < 500000; iTunes + {s = strings.Join ([] string {s, "motong"}, ")} T2: = time.Now (). UnixNano () / 1e6 fmt.Println (" T2 = ", T2) fmt.Println (" t2-t1 = = ", t2-t1)}

The program runs to see the effect:

# go run main.go

T1 = = 1634379455304

T2 = = 1634379598227

T2-T1 = = 142923

It takes 142923 ms for a total of 2 minutes and 22 seconds, which is about the same as using +.

The way you use buffer

It should be said that using buffer is the best way.

Func main () {T1: = time.Now (). UnixNano () / 1e6 fmt.Println ("T1 = =", T1) s: = bytes.NewBufferString ("xiao") for I: = 0; I < 500000; iTunes + {s.WriteString ("motong")} T2: = time.Now () .UnixNano () / 1e6 fmt.Println ("T2 =", T2) fmt.Println ("t2-t1 =", t2-t1)}

# go run main.go

T1 = = 1634378506021

T2 = = 1634378506030

T2-T1 = = 9

Through the above data, we can see that the same 500000 times of data are stitched together.

The first, using the + method, requires 147477 ms

Second, using fmt.Sprintf () requires 262931 ms

Third, the way to use strings.Join requires 142923 ms

Fourth, the way to use buffer requires 9ms

The way of using buffer is 16386 times that of the first one, 29214 times that of the second one, and 15880 times that of the third one.

These are all the contents of the article "what are the details of efficient coding in golang". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report