整数自动拆箱和自动装箱会产生性能问题吗? [英] Integer auto-unboxing and auto-boxing gives performance issues?

查看:135
本文介绍了整数自动拆箱和自动装箱会产生性能问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在使用 x ++; 进行一些迭代和其他操作,其中 x 整数而不是 int

We are currently doing some iterations and other operations using x++; where x is an Integer and not an int.

在我们系统的某些用户操作中可能会重复操作,但没有太多复杂或像数学应用程序那样繁多,每个用户事务最多可达10000次..

Operations may be repeated throughout some user operations on our system but nothing too complex or numerous like a Mathematical application, maximum up to 10000 times per user transaction..

这种拆箱和后来的装箱是否会影响我们的性能毫秒

Will this unboxing and later boxing affect our performance by some noticeable milliseconds?

推荐答案

http://download.oracle.com/ javase / 1.5.0 / docs / guide / language / autoboxing.html

结果列表的性能可能很差,因为它是盒子或者在每次获取或设置操作时都是unboxes。它足够快,偶尔可以使用,但在性能关键的内循环中使用它会很愚蠢。

"The performance of the resulting list is likely to be poor, as it boxes or unboxes on every get or set operation. It is plenty fast enough for occasional use, but it would be folly to use it in a performance critical inner loop.

所以当你应该使用自动装箱和拆箱吗?只有当参考类型和基元之间存在阻抗不匹配时才使用它们,例如,当你必须放置数字时将值映射到集合中。将自动装箱和拆箱用于科学计算或其他对性能敏感的数字代码是不合适的。 Integer不能替代int; autoboxing和unboxing模糊了原始类型和引用类型之间的区别,但它们并没有消除它。

So when should you use autoboxing and unboxing? Use them only when there is an "impedance mismatch" between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it."

这篇关于整数自动拆箱和自动装箱会产生性能问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆