In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what are the traps of if-else and Switch structure in high concurrency. It is very detailed and has certain reference value. Friends who are interested must finish it!
Swich's pit, as soon as the environment changes, the efficiency will be much worse.
Since there is no Switch in Rust, the following code is temporarily demonstrated in the GE language. Let's take a look at the following code:
Package main import ("fmt"math/rand" / / "sync"time") func main () {now: = time.Now () .UnixNano () count: = [] int {0,0,0,0,0} for I: = 0; I < 100000 Random: = rand.Intn switch random {case 1: count [1] + + case 2: count [2] + + case 3: count [3] + + Case 4: count [4] + + case 5: count [5] + + default: count [0] + +}} fmt.Println (time.Now () .UnixNano () -now) fmt.Println (count)}
Its operation results are as follows:
[root@ecs-a4d3 hello_world] # time. / test12818084 [99507 86 108 106 96 97] real 0m0.004suser 0m0.003ssys 0m0.001s
Let's slightly change the range of Random from 100 to 5.
Package mainimport ("fmt"math/rand" / / "sync"time") func main () {now: = time.Now () .UnixNano () count: = [] int {0,0,0,0,0} for I: = 0; I < 100000 Switch random + {random: = rand.Intn (1000) switch random {case 1: count [1] + + case 2: count [2] + + case 3: count [3] + + Case 4: count [4] + + case 5: count [5] + + default: count [0] + +}} fmt.Println (time.Now () .UnixNano () -now) fmt.Println (count)}
The running result of the above code is as follows:
[root@ecs-a4d3 hello_world] # time. / test1 4365712 [20184 20357 19922 19897 19640 0] real 0m0.006s user 0m0.004s sys 0m0.002s
It can be seen that the running time of these two programs varies by more than 30%, and this result is really frightening for careful consideration, because there is no change in the actual code execution logic at all, only the range of variables has been adjusted. It will make the running efficiency of the program very different, that is, when the system encounters some extreme situations, the running time of the same program is very different.
Rust's Else if is the same.
Of course, when we say that switch is not good, it does not mean that elseif avoids this problem. According to the principle of instruction pipeline, elseif is also the same when dealing with branches, so Rust does not recommend the writing of elseif. Take Rust as an example:
Use rand::Rng; fn main () {let mut rng = rand::thread_rng (); let mut arr = [0arr 0,0]; / / println! ("Integer: {}", arr [random]); for i in 0.. 1000000 {let mut random= rng.gen_range (0Power5); if random==0 {arr [0] = arr [0] + 1;} else if random==1 {arr [1] = arr [1] + 1;} else if random==2 {arr [2] = arr [2] + 1 } else if random==3 {arr [3] = arr [3] + 1;} else if random==4 {arr [4] = arr [4] + 1;} else {arr [5] = arr [5] + 1;}} println! ("{}, {}", arr [0], arr [1], arr [2], arr [3], arr [4], arr [5]);}
Adjust the range of random numbers
Let mut random = rng.gen_range (0100)
It can be observed that the time difference between the two programs is nearly 40%. This result also profoundly illustrates a problem. This example actually simulates an extreme state, that is, if the value of a variable suddenly changes from 0-100 to 0-5, then the running efficiency of the program may change greatly. The corollary is that once the system runs in a certain extreme state, For example, if the percentage of errors is too high or other extreme scenarios, then the normal stress test results may not be telling the problem at all.
These are all the contents of this article entitled "what are the traps of if-else and Switch structures in high concurrency?" Thank you for reading! Hope to share the content to help you, more related 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.