xdm, we all know that golang is a naturally highly concurrent and efficient compiled language
But we all know that no matter how good the tool is, if the usage is wrong, it will be in vain. Let’s take two common paths to experience it.
Who should I use struct and map?
When the calculation volume is very small, the time-consuming difference between using temporary struct and map may not be seen, but when the number increases, the gap becomes obvious, and the gap becomes more and more obvious as the number increases.
When we encounter that both keys and values can be fixed, we are much more efficient in choosing struct than in choosing map
- We simulate a loop to calculate 100 million times to see how long it takes to use their respective data structures
- Calculate the current time before looping
- Calculate the current time after the loop
- Finally, we calculate the difference between the two times, here we use milliseconds as units
func main() { t1 :=().UnixNano()/1e6 for i := 0; i < 100000000; i++ { var test struct { Name string hobby string } = "xiaomotong" = "program" } t2 :=().UnixNano()/1e6 ("t2 - t1 == ", t2-t1) }
The program runs to view the effect:
# go run
t1 == 1634377149185
t2 == 1634377149221
t2 - t1 == 36
Using struct takes 36 ms. What do you think of this time?
Let's take a look at how to use map
func main() { t1 :=().UnixNano()/1e6 ("t1 == ", t1) for i := 0; i < 100000000; i++ { var test = map[string]interface{}{} test["name"] = "xiaomotong" test["hobby"] = "program" } t2 :=().UnixNano()/1e6 ("t2 == ", t2) ("t2 - t1 == ", t2-t1) }
The program runs to view the effect:
# go run
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 212 times. In this way, when we usually encode, which data structure would you choose for the above scenarios?
Why is the above gap so big?
In the case where we can determine the field, we use temporary Struct to not dynamically allocate content during runtime.
But map is different. Map also needs to check indexes, which is very time-consuming.
How to splice strings?
How do I implement the encoding xdm when I encounter string splicing? Our tools are temporarily provided as follows:
- How to use +
- How to use ()
- How to use
- How to use buffer
Seeing this, maybe we each have their own answers, but let's do it and see how their respective processing takes when they are splicing the same string.
Using +
Let's calculate the loop append 500,000 strings to see how long it takes
func main() { t1 := ().UnixNano() / 1e6 ("t1 == ", t1) s := "xiao" for i := 0; i < 500000; i++ { s += "motong" } t2 := ().UnixNano() / 1e6 ("t2 == ", t2) ("t2 - t1 == ", t2-t1) }
The program runs to view the effect:
# go run
t1 == 1634378595642
t2 == 1634378743119
t2 - t1 == 147477
Did you be shocked to see this data? It's so slow and takes 147477 ms. That's exactly 2 minutes and 27 seconds.
Using + processing strings in Go language is very performance-consuming, we can see through the data
How to use ()
func main() { t1 := ().UnixNano() / 1e6 ("t1 == ", t1) s := "xiao" for i := 0; i < 500000; i++ { s = ("%s%s",s,"motong") } t2 := ().UnixNano() / 1e6 ("t2 == ", t2) ("t2 - t1 == ", t2-t1) }
The program runs to view the effect:
# go run
t1 == 1634378977361
t2 == 1634379240292
t2 - t1 == 262931
We were also shocked when we saw this data. It took 262931 ms, a total of 4 minutes and 22 seconds. Didn’t we expect that using xdm is slower than using +
How to use
func main() { t1 := ().UnixNano() / 1e6 ("t1 == ", t1) s := "xiao" for i := 0; i < 500000; i++ { s = ([]string{s,"motong"},"") } t2 := ().UnixNano() / 1e6 ("t2 == ", t2) ("t2 - t1 == ", t2-t1) }
The program runs to view the effect:
# go run
t1 == 1634379455304
t2 == 1634379598227
t2 - t1 == 142923
It takes 142923 ms, a total of 2 minutes and 22 seconds, which is comparable to using +
How to use buffer
The best way to use buffer is,
func main() { t1 := ().UnixNano() / 1e6 ("t1 == ", t1) s := ("xiao") for i := 0; i < 500000; i++ { ("motong") } t2 := ().UnixNano() / 1e6 ("t2 == ", t2) ("t2 - t1 == ", t2-t1) }
# go run
t1 == 1634378506021
t2 == 1634378506030
t2 - t1 == 9
Through the above data, we see that the same data spliced 500,000 times
- The first method, using +, requires 147477 ms
- The second method, using () requires 262931 ms
- The third way to use , requires 142923 ms
- The fourth method, using buffer, requires 9ms
The way to use buffer is 16,386 times that of the first type, 29,214 times that of the second type, and 15,880 times that of the third type
xdm, if you encounter the above scenario, which method would you choose to use?
This is the end of this article about the details of efficient coding of golang. For more relevant efficient coding content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!