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 or 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 is good
How do I implement the encoding xdm when I encounter string splicing? Our tools are temporarily provided as follows:
- use
+
Way - use
()
Way - use
Way
- use
buffer
Way
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.
Used in Go+
Processing strings 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 think of using xdm?+
Still slow
How to use
func main() { t1 := ().UnixNano() / 1e6 ("t1 == ", t1) s := []string{} s = append(s,"xiao") for i := 0; i < 500000; i++ { s = append(s ,"motong") } (s,"") t2 := ().UnixNano() / 1e6 ("t2 == ", t2) ("t2 - t1 == ", t2-t1) }
The program runs to view the effect:
# go run
t1 == 1634570001216
t2 == 1634570001294
t2 - t1 == 78
Time to take 142923 ms, total ** 78 ms**
How to use buffer
usebuffer
The best way to do it 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 one is used
+
The way, 147477 ms are needed - The second type is used
()
The way, it requires 262931 ms - The third type is used
The method, requires 78 ms
- The fourth type is used
buffer
, 9ms
How to use buffer
It is 16,386 times the first type, 29,214 times the second type, and more than 8 times the third type
xdm, if you encounter the above scenario, which method would you choose to use? You can discuss it in the comment area. Is there a more efficient way?
This is the end of this article about the details of efficient coding in Go. 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!