本文翻译自:The performance impact of using instanceof in Java

I am working on an application and one design approach involves extremely heavy use of the instanceof operator. 我正在开发一个应用程序,一种设计方法涉及极大地使用instanceof运算符。 While I know that OO design generally tries to avoid using instanceof , that is a different story and this question is purely related to performance. 虽然我知道OO设计通常会试图避免使用instanceof ,但这是一个不同的故事,这个问题纯粹与性能有关。 I was wondering if there is any performance impact? 我想知道是否有任何性能影响? Is is just as fast as == ? 是和==一样快吗?

For example, I have a base class with 10 subclasses. 例如,我有一个包含10个子类的基类。 In a single function that takes the base class, I do checks for if the class is an instance of the subclass and carry out some routine. 在一个获取基类的函数中,我会检查该类是否是子类的实例并执行一些例程。

One of the other ways I thought of solving it was to use a "type id" integer primitive instead, and use a bitmask to represent categories of the subclasses, and then just do a bit mask comparison of the subclasses "type id" to a constant mask representing the category. 我想解决它的另一种方法是使用“type id”整数原语,并使用位掩码来表示子类的类别,然后只需对子类“type id”进行掩码比较。表示类别的常量掩码。

Is instanceof somehow optimized by the JVM to be faster than that? 以某种方式由JVM优化的instanceof比这更快吗? I want to stick to Java but the performance of the app is critical. 我想坚持使用Java,但应用程序的性能至关重要。 It would be cool if someone that has been down this road before could offer some advice. 如果之前一直走在这条路上的人可以提供一些建议,那将会很酷。 Am I nitpicking too much or focusing on the wrong thing to optimize? 我是在挑剔太多还是专注于错误的事情来优化?


#1楼

参考:https://stackoom.com/question/QwO/在Java中使用instanceof的性能影响


#2楼

Modern JVM/JIC compilers have removed the performance hit of most of the traditionally "slow" operations, including instanceof, exception handling, reflection, etc. 现代JVM / JIC编译器已经消除了大多数传统上“慢”操作的性能损失,包括实例,异常处理,反射等。

As Donald Knuth wrote, "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." 正如唐纳德·克努特(Donald Knuth)所写的那样,“我们应该忘记小的效率,大约97%的时间说:过早的优化是所有邪恶的根源。” The performance of instanceof probably won't be an issue, so don't waste your time coming up with exotic workarounds until you're sure that's the problem. instanceof的性能可能不会成为一个问题,所以不要浪费你的时间来提出异乎寻常的解决方法,直到你确定这是问题为止。


#3楼

Generally the reason why the "instanceof" operator is frowned upon in a case like that (where the instanceof is checking for subclasses of this base class) is because what you should be doing is moving the operations into a method and overridding it for the appropriate subclasses. 通常,“instanceof”运算符在类似情况下(其中instanceof正在检查此基类的子类)不赞成的原因是因为您应该做的是将操作移动到方法中并将其覆盖为适当的子类。 For instance, if you have: 例如,如果你有:

if (o instanceof Class1)doThis();
else if (o instanceof Class2)doThat();
//...

You can replace that with 你可以用它替换它

o.doEverything();

and then have the implementation of "doEverything()" in Class1 call "doThis()", and in Class2 call "doThat()", and so on. 然后在Class1调用“doThis()”中执行“doEverything()”,在Class2调用“doThat()”中执行,依此类推。


#4楼

'instanceof' is actually an operator, like + or -, and I believe that it has its own JVM bytecode instruction. 'instanceof'实际上是一个运算符,如+或 - ,我相信它有自己的JVM字节码指令。 It should be plenty fast. 它应该足够快。

I should not that if you have a switch where you are testing if an object is an instance of some subsclass, then your design might need to be reworked. 我不应该如果你有一个开关来测试一个对象是否是某个子类的实例,那么你的设计可能需要重新设计。 Consider pushing the subclass-specific behavior down into the subclasses themselves. 考虑将子类特定的行为向下推入子类本身。


#5楼

Instanceof is very fast. Instanceof非常快。 It boils down to a bytecode that is used for class reference comparison. 它归结为一个字节码,用于类参考比较。 Try a few million instanceofs in a loop and see for yourself. 在循环中尝试几百万个instanceof并亲自看看。


#6楼

You should measure/profile if it's really a performance issue in your project. 如果它确实是项目中的性能问题,您应该测量/分析。 If it is I'd recommend a redesign - if possible. 如果是的话,我建议重新设计 - 如果可能的话。 I'm pretty sure you can't beat the platform's native implementation (written in C). 我很确定你无法击败平台的原生实现(用C语言编写)。 You should also consider the multiple inheritance in this case. 在这种情况下,您还应该考虑多重继承。

You should tell more about the problem, maybe you could use an associative store, eg a Map<Class, Object> if you are only interested in the concrete types. 你应该告诉更多关于这个问题的信息,也许你可以使用一个关联商店,例如Map <Class,Object>,如果你只对具体类型感兴趣的话。

在Java中使用instanceof的性能影响相关推荐

  1. java instanceof性能差_在J中使用instanceof的性能影响

    在J中使用instanceof的性能影响 我正在开发一个应用程序,一种设计方法涉及到instanceof运算符的极大使用. 虽然我知道OO设计通常会试图避免使用instanceof,但这是一个不同的故 ...

  2. 在Java里面使用instanceof的性能影响

    问题:在Java里面使用instanceof的性能影响 我正在写一个应用程序,其中一种设计方案包含了instanceof操作的大量使用.虽然我知道面向对象设计通常试图避免使用instanceof,但那 ...

  3. java overdata_使用try-catch over条件来安全地设置值,并在java中以最小的性能影响...

    在这里,我的主要目标是安全地设置值,而不会产生性能(速度,内存,CPU等)影响. 我有一个愚蠢的选择(坏的风格)也在下面提到. 那么,最好的方法是什么? 选项1? 选项2? 还是另一个? 选项1 : ...

  4. Java中的instanceof关键字

    Java中的instanceof关键字 instanceof是Java的一个二元操作符,和==,>,<是同一类东东.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是测试它左边 ...

  5. java中的instanceof的用法

    java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例.  用法: re ...

  6. java 中的instanceof

    java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法: res ...

  7. 品味性能之道十一:JAVA中switch和if性能比较

    通常而言大家普遍的认知里switch case的效率高于if else.根据我的理解而言switch的查找类似于二叉树,if则是线性查找.按照此逻辑推理对于对比条件数目大于3时switch更优,并且对 ...

  8. Java中的Instanceof

    java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例.  用法:res ...

  9. java中的native方法性能到底怎么样?

    前言 java中的native方法性能到底怎么样? 第一次写博客,如果写的不好,望见谅,烦请指出问题,虚心学习 先说结论,native 方法性能不如java方法 一.native方法? 主要是java ...

最新文章

  1. 曾大战LeCun的谷歌女性科学家,刚刚被Jeff Dean开除了!
  2. 数据导出到Excel
  3. Docker搭建NSQ实时分布式消息集群
  4. Linux下C++ UDP Socket例子
  5. mysql-5.5.8_MySQL5.5.8安装
  6. feign post 传递空值_听我讲完GET、POST原理,面试官给我倒了杯卡布奇诺
  7. 4.4.3 日期与时间计算
  8. CentOS查看分区的方式
  9. vasp软件全名是什么_Materials Studio软件常见问题与解答
  10. mysql ddl 进度_MySQL DDL详情揭露
  11. zip压缩脚本(linux)
  12. Shape—自定义图片(详细讲解)
  13. Python案例—AQI 空气质量指数
  14. 普通table表格样式大全
  15. mysql索引linke和等于_MySQL索引介绍和实战
  16. linux查看riak版本,Riak学习(一):Linux Centos 下安装 Riak 服务
  17. html中label如何居中,怎么让label标签中的文字居中显示
  18. 大一上學期學習生活情況總結
  19. 【win7怎么不让垃圾文件进回收站删除】
  20. python中pandas检索某一个具体值(具体到一个元素)

热门文章

  1. 51Nod 1256 乘法逆元 Label:exgcd
  2. WebGIS中利用AGS JS+eCharts实现一些数据展示的探索
  3. Apache Spark源码走读之22 -- 浅谈mllib中线性回归的算法实现
  4. [20] 鼓状物(Drum)图形的生成算法
  5. 删除文件夹(包括子文件夹)
  6. 093-PHP数组比较
  7. C# -- 冒泡排序
  8. Keras 构建DNN 对用户名检测判断是否为非法用户名(从数据预处理到模型在线预测)...
  9. 手机Web 开发中图片img 如何等比例缩放
  10. 20171016课程随笔