当前位置:网站首页>不用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,需要从内存重新读入分页,导致进入操作系统内核态
边栏推荐
- 《跟唐老师学习云网络》 - 问题定位 - 主机通但容器不通
- Security tip: FreeType in Qt
- Beyond Stream Processing !第四届实时计算 Flink 挑战赛启动,49 万奖金等你来拿!
- 360闷声干大事获赞无数,数字安全如何保障?还得看企业安全云
- 数据库性能系列之索引(上)
- Database dirty reads, non-repeatable reads, phantom reads and corresponding isolation levels
- 鸿湖万联扬帆富设备开发板正式合入OpenHarmony主干
- MySQL——数据库基础
- 原生js 创建表格
- 干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
猜你喜欢
数据库性能系列之索引(上)
Current relay JL-8GB/11/AC220V
Easy gene: human tRNA gene loci showed age-related high DNA methylation | research articles
idea的package没有空心
Classes and Objects - 6 Default Member Functions
208. 实现 Trie (前缀树)
Linux内核设计与实现(十)| 页高速缓存和页回写
Neural Ordinary Differential Equations
Neural Network Study Notes 3 - LSTM Long Short-Term Memory Network
Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions
随机推荐
oracle export dmp file type as "crash dump file"
神经网络学习笔记4——自动编码器(含稀疏,堆叠)(更新中)
Drools 规则引擎一文读懂
Swift 常用扩展类和简单封装
【云原生】-Docker安装部署分布式数据库 OceanBase
Vim plugin GrepIt
电压继电器HDY-A/1-220VAC-1
Linux内核设计与实现(十)| 页高速缓存和页回写
Database transactions, JDBC operations and data types
获取1688app上原数据 API
Easy gene: human tRNA gene loci showed age-related high DNA methylation | research articles
Static LED display developed by single chip microcomputer
TensorFlow自定义训练函数
AIX shell获取前几个月时间
oracle 导出dmp文件类型为“故障转储文件”
vscode中写markdown格式笔记的配置过程和相关语法
208. 实现 Trie (前缀树)
Database dirty reads, non-repeatable reads, phantom reads and corresponding isolation levels
@RequestBody 和 @ResponseBody 详解
Hu-cang integrated e-commerce project (1): project background and structure introduction