当前位置: 首页 > news >正文

代码与细节(一)

在用到 Java17的新特性 Unmodifiable Lists 时不知道你是否和我有同样的惊讶

为什么弄了这么多重载方法?

先说结论:为了性能。

其实一细想,都能想明白:varargs(可变参数) 的背后是数组的内存分配和初始化,相比正常的传参涉及更多的内存开销。

所以任何便利都是有代价的。下面列出一段大神兼偶像 Joshua Bloch 《Effective Java - Third Edition》Item53:Use varargs judiciously 的原文作为佐证:

Exercise care when using varargs in performance-critical situations. Every invocation of a varargs method causes an array allocation and initialization. If you have determined empirically that you can't afford this cost but you need the flexibility of varargs, there is a pattern that lets you have your cake and eat it too.
Suppose you've determined that 95 percent of the calls to a method have three or fewer parameters. Then declare five overloadings of the method, one each with zero through three ordinary parameters, and a single varargs method for use when the number of arguments exceeds three:

public void foo() { }
public void foo(int a1) {}
public void foo(int a1, int a2) { }
public void foo(int a1, int a2, int a3) {}
public void foo(int a1, int a2, int a3, int... rest) { }

http://www.lryc.cn/news/198723.html

相关文章:

  • AI绘画使用Stable Diffusion(SDXL)绘制中国古代神兽
  • 老卫带你学---leetcode刷题(148. 排序链表)
  • 21.1 stm32使用LTDC驱动LCD--配置说明
  • zabbix监控nginx的状态页面
  • C语言初学者工具选择:vscode + MSYS2 + cmake 搭建 C环境
  • 【四:httpclient的使用】
  • 在innodb引擎中,count(*)、count(1)、count(主键)、count(字段)哪个性能最高?
  • 华为OD 跳格子2(200分)【java】B卷
  • javascript/python 笔记: folium feature group自动切换
  • Python中的元组
  • 在云计算环境中,如何利用 AI 改进云计算系统和数据库系统性能
  • OpenP2P实现内网穿透远程办公
  • 黑白棋(Othello, ACM/ICPC World Finals 1992, UVa220)rust解法
  • MySQL中如何进行表的优化和压缩?
  • 【Java】Jsoup格式化html问题(文本空格折叠等)解决方法
  • Ansible定义各类变量,引用变量方式介绍及注册变量和vars_prompt的用法示例
  • 各类证件的版面信息收集
  • vueday01——ref响应式
  • SpringBoot集成Redisson操作Redis
  • 整数反转
  • 【ELK使用指南 2】常用的 Logstash filter 插件详解(附应用实例)
  • (转)STR 内核做了什么
  • fastapi项目结构以及多进程部署
  • 【LeetCode】 412. Fizz Buzz
  • vector+算法sort与list+sort的效率比较,容易写错的地方原因探析
  • iOS——Manager封装网络请求
  • 【javascript】内部引入与外部引入javascript
  • 掌握JavaScript的练习之道:十个手写函数让你信手拈来!
  • 买卖股票的最佳时机 II[中等]
  • 前端开发调试技巧:如何在Component下选中当前插件并且查看当前插件信息