当前位置:网站首页>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 == 3
First 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
边栏推荐
- eric6教程(电脑的配置基本知识)
- TensorFlow custom training function
- 京东校招笔试题+知识点总结
- TensorFlow custom training function
- Bagging-Blending Multi-Model Fusion Short-Term Electricity Load Forecasting Based on Weighted Grey Correlation Projection
- UE5 GAS 学习笔记 后记0
- Microsoft SQL server hacked, bandwidth stolen
- VLAN相关知识点
- [ASP.NET Core] Dependency Injection for Option Classes
- 电压继电器HDY-A/1-220VAC-1
猜你喜欢
随机推荐
oracle export dmp file type as "crash dump file"
Based on sliding mode control of uncertain neutral system finite time stable
编译Hudi
Typroa 替代工具marktext
LeetCode_236_二叉树的最近公共祖先
电流继电器JL-8GB/11/AC220V
Transfer Learning技术研修
VSCode更改插件的安装位置
Performance testing of API Gateway APISIX on Google Cloud T2A and T2D
Typroa alternative tool marktext
高手云集、丰富活动,斩获佳绩,超过2万名开发者参与的AI社团邀你加入!
ODrive应用 #4 配置参数&指令「建议收藏」
湖仓一体电商项目(一):项目背景和架构介绍
TensorFlow自定义训练函数
UE5 GAS Study Notes Postscript 0
stm32 RTC闹钟唤醒低功耗模式
【JZ64 求1+2+3+...+n】
salesforce使用方法(salesforce authenticator下载)
流水线上的农民:我在工厂种蔬菜
PanGu-Coder: Function-level code generation model