当前位置:网站首页>不用if分支对同一个变量做判断的方法
不用if分支对同一个变量做判断的方法
2022-07-30 11:07:00 【冬眠的关刀】
对于多条件采用同一变量,取等判断的问题
if a == 1
else if a == 2
else if a == 3
首先,观感上优化,要把1,2,3这种魔法数字,转化为枚举类型,用有含义的命名来规范,要比直接使用魔法数字好的多。
其次,性能上优化,把多if分支,改为hash方法,比如说用一张hashmap的key来存所有的1,2,3,把1,2,3执行的函数,分别写三个函数,把这三个函数的对象存在hashmap的value中,建立映射。
很多软件项目都喜欢用这样的设计方法,
但是这种方法很多情况下也有局限性 ,可能最终的性能优化只是自欺欺人,
把函数执行离散化的同时,也导致函数在内存上的分布更离散,在函数量很大的情况下,有可能导致CPU Cache Miss,需要从内存重新读入分页,导致进入操作系统内核态
边栏推荐
- idea的package没有空心
- Current relay JL-8GB/11/AC220V
- 易基因:人类tRNA基因位点表现出与衰老相关的DNA高甲基化|研究文章
- "Learning Cloud Networking with Teacher Tang" - Problem Location - The host is working but the container is not working
- HJY-F931A/YJ三相电压继电器
- Voltage relay HDY - vac - 1 A / 1-220
- 分布式限流 redission RRateLimiter 的使用及原理
- oracle export dmp file type as "crash dump file"
- 208. 实现 Trie (前缀树)
- 淘宝/天猫淘宝评论问答列表接口 API
猜你喜欢

VLAN实验

360发布面向未来的EDR,全方位守护政企用户终端安全

360闷声干大事获赞无数,数字安全如何保障?还得看企业安全云

VSCode更改插件的安装位置

Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions

The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust

High energy output!Tencent's internal MyCat middleware manual, both theoretical and practical

API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试

我又造了个轮子:GrpcGateway

Leetcode 125. 验证回文串
随机推荐
Taobao/Tmall taobao comments q&a list interface API
typescript入门之helloworld
VSCode更改插件的安装位置
优酷VIP会员周卡只需7.5元,看《沉香如屑》用优酷视频
@RequestBody 和 @ResponseBody 详解
Vim plugin GrepIt
Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions
HJY-F931A/YJ三相电压继电器
加密和安全
神经网络学习笔记3——LSTM长短期记忆网络
jmeter接口压力测试-(二)
【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
高手云集、丰富活动,斩获佳绩,超过2万名开发者参与的AI社团邀你加入!
Database dirty reads, non-repeatable reads, phantom reads and corresponding isolation levels
图像去噪——Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images
Oracle中SQL语言和分页rownum分析
Hu-cang integrated e-commerce project (1): project background and structure introduction
feign远程调用时如何在请求头加入数据
数据库事务,JDBC操作和数据类型
NLP领域的最新研究进展