当前位置:网站首页>The method of judging the same variable without the if branch
The method of judging the same variable without the if branch
2022-07-30 11:47:00 【hibernating knives】
For the problem of using the same variable for multiple conditions, taking equal judgment
if a == 1else if a == 2else if a == 3First of all, to optimize the look and feel, it is better to convert magic numbers such as 1, 2, 3 into enumeration types and use meaningful names to standardize them, rather than using magic numbers directlymany.
Secondly, to optimize the performance, change the multi-if branch to the hash method, for example, use a hashmap key to store all 1, 2, 3, change 1,2, 3 The functions executed by , write three functions respectively, store the objects of these three functions in the value of the hashmap, and establish a mapping.
Many software projects like to use this design method,
but this method also has limitations in many cases, maybe the final performance optimization is just self-deception,
discrete function executionAt the same time, it also causes the distribution of functions in memory to be more discrete. In the case of a large amount of functions, it may lead to CPU Cache Miss, which needs to be read into the paging from the memory again, resulting in entering the operating system kernel state
边栏推荐
猜你喜欢

数据库性能系列之索引(上)

Verilog语法基础HDL Bits训练 08

HJY-F931A/YJ three-phase voltage relay

idea的package没有空心

STM32F1 reads MLX90632 non-contact infrared temperature sensor

Redis 主从复制

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

ansible学习笔记01

周鸿祎:微软抄袭了360安全模式 所以成为美国最大的安全公司

VLAN实验
随机推荐
我又造了个轮子:GrpcGateway
Verilog grammar basics HDL Bits training 08
面试官:Redis中的布隆过滤器与布谷鸟过滤器,你了解多少?
LeetCode_235_二叉搜索树的最近公共祖先
EA中的业务对象和业务实体你分得清吗?
优酷VIP会员周卡只需7.5元,看《沉香如屑》用优酷视频
FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)
不用if分支对同一个变量做判断的方法
I built another wheel: GrpcGateway
Leetcode 125. 验证回文串
Differences between lock spin and mutex usage scenarios
ODrive应用 #4 配置参数&指令「建议收藏」
Verilog语法基础HDL Bits训练 08
零代码开发入门:快速上手DIY函数公式的5个步骤
原生js 创建表格
【ASP.NET Core】选项类的依赖注入
Voltage relay h2d SRMUVS - 100 vac - 2
Transfer Learning Technology Training
VSCode更改插件的安装位置
The package of idea is not hollow