温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

高并发中if-else、Switch结构的陷阱有哪些

发布时间:2021-09-09 10:57:08 来源:亿速云 阅读:138 作者:小新 栏目:编程语言

这篇文章主要介绍高并发中if-else、Switch结构的陷阱有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Swich的坑,环境一变效率就差远了

由于Rust并没有Switch了,因此以下代码就暂用Go语言来演示了。我们先来看以下这段代码:

package main
 
import (
        "fmt"
        "math/rand"
 
        //"sync"
 
        "time"
)

 
func main() {
 
        now := time.Now().UnixNano()
 
        count := []int{0, 0, 0, 0, 0, 0}
 
        for i := 0; i < 100000; i++ {
 
                 random := rand.Intn(100)
 
                 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)
 
 
 
}

它的运行结果如下:

[root@ecs-a4d3 hello_world]# time ./test1
2818084
[99507 86 108 106 96 97] 
real  0m0.004s
user 0m0.003s
sys   0m0.001s

我们再稍微把Random的范围由100改为5。

package main

import (
 
        "fmt"
 
        "math/rand"
 
 
 
        //"sync"
 
        "time"
 
)
 
 
 
func main() {
 
        now := time.Now().UnixNano()
 
        count := []int{0, 0, 0, 0, 0, 0}
 
 
 
        for i := 0; i < 100000; i++ {
 
                 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)
 
 
 
}

上述的代码运行结果如下:

[root@ecs-a4d3 hello_world]# time ./test1
 
4365712
 
[20184 20357 19922 19897 19640 0]
 
 
 
real  0m0.006s
 
user 0m0.004s
 
sys   0m0.002s

可以看到这两段程序的运行时间相差了30%多,这个结果真的是细思极恐,因为也就是实际代码执行逻辑完全没有任何变化 ,只是变量取值范围有所调整,就会使程序的运行效率大为不同,也就是说当系统遭遇到某些极端情况时,同样的程序所需要的运行时间却有天壤之别。

Rust的Else if也是一样的坑

当然我们说switch不好也就不是说else if就避免了这个问题,根据指令流水线的原理,elseif在处理分支时情况也一样,因此Rust也不太推荐else if的写法,以Rust为例如下:

use rand::Rng;
 
fn main() {
 
let mut rng = rand::thread_rng();
 
let mut arr = [0, 0, 0, 0, 0,0];
 
//println!("Integer: {}", arr[random]);
 
for i in 0..1000000 {
 
let mut random =rng.gen_range(0,5);
 
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]);
 
}

将随机数的取值范围调整一下

let mut random =rng.gen_range(0,100);

可以观察到这两段程序运行的时间也要相差近40%,这样的结果也深刻说明了一个问题,这个例子其实模拟的是一个极端状态,也就是某一个变量取值突然从0-100变成了0-5那么程序的运行效率可能就会有极大的改变,这个推论就是一旦系统运行在某一个极端状态,比如错误占比过高或者其它极端场景,那么正常情况下的压力测试结果也许就完全不能说明问题了。

以上是“高并发中if-else、Switch结构的陷阱有哪些”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI