在应用程序中过于频繁地使用非原始整数/长数据类型会损害性能吗? [英] Can using non primitive Integer/ Long datatypes too frequently in the application, hurt the performance?

查看:22
本文介绍了在应用程序中过于频繁地使用非原始整数/长数据类型会损害性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中经常使用 Long/Integer 数据类型来构建通用数据类型.我担心使用这些包装器对象而不是原始数据类型可能对性能有害,因为每次都需要创建一个昂贵的操作的对象.但似乎我别无选择(当我必须将基元与泛型一起使用),而不仅仅是使用它们.
但是,如果您能建议我是否可以做些什么来使它变得更好,那就太好了.或者有什么办法可以避免它??

I am using Long/Integer data types very frequently in my application, to build Generic datatypes. I fear that using these wrapper objects instead of primitive data types may be harmful for performance since each time it needs to create objects which is an expensive operation. but also it seems that I have no other choice(when I have to use primtives with generics) rather than just using them.
However, still it would be great if you can suggest if there is anything I could do to make it better. or any way if I could just avoid it ??

还有什么缺点?

欢迎提出建议!

推荐答案

在我之后重复.在 Java 中创建对象并不是一项昂贵的操作".

您过早地优化了您的应用程序.更好的方法是使用 IntegerLong 以自然的方式实现它,然后对其进行分析以确定瓶颈所在.如果分析器告诉您使用 IntegerLong 是一个性能问题,然后看看如何解决这个问题.

You are prematurely optimizing your application. A better approach is to implement it in the natural way using Integer and Long, then profile it to determine where the bottlenecks are. If the profiler tells you that use of Integer and Long is a performance issue, then look at ways to cure this.

如果您确定 IntegerLong 确实存在问题,您可以执行以下操作:

If you determine that Integer and Long really are an issue, here are some things you could do:

  • 寻找实现原始类型集合"的类库;例如宝藏.但请注意,此类集合类型的 API 不会java.util.Collection 及其后代兼容.

使用 Integer.valueOf(int)Long.valueOf(long) 而不是 new Integer(int) 和 <代码>新长(长).valueOf 方法使用常用对象的缓存来减少对象创建的次数.

Use Integer.valueOf(int) and Long.valueOf(long) rather than new Integer(int) and new Long(long). The valueOf methods use a cache of frequently used objects to reduce the number of object creations.

@Rex Kerr 的评论是这是一个可怕的建议.他(我认为)说 OP 应该优化他的应用程序以减少 IntegerLong 的使用,然后他知道这将是一个性能问题.我不同意.

@Rex Kerr's comment is that this is horrible advice. He is (I think) saying that the OP should optimize his application to reduce the use of Integer and Long before he knows that this will be a performance concern. I disagree.

  • 此时(当他提出问题时),OP 不知道他的应用程序需要优化.如果应用程序在没有任何优化的情况下运行得足够快",那么开发人员花在优化上的时间最好花在其他方面.

  • At this point (when he asked the question), the OP didn't know that his application needed optimization. If the application runs "fast enough" without any optimization, then any developer time spent optimizing it would be better spent on something else.

此时,OP 不知道性能瓶颈在哪里.如果他们不在处理这些值,那么优化这方面将是浪费时间.请注意,一般来说,完全依靠您的直觉来告诉您瓶颈在哪里或可能在哪里是一个坏主意.

At this point, the OP doesn't know where the performance bottlenecks are. If they are not in the handling of these values, then optimizing this aspect will be a waste of time. Note that generally speaking it is a bad idea to rely solely on your intuition to tell you where the bottlenecks are or are likely to be.

@Rex Kerr 认为修改/重组代码以解决由于过度使用 IntegerLong.这根本不是真的.一个不错的 IDE 可以轻松地在中小型应用程序中进行此类更改.

@Rex Kerr posits that it would be a lot of work to modify/restructure the code to fix performance issues due to over-use of Integer and Long. That's simply not true. A decent IDE makes it easy to make this sort of change in a small to medium size application.

这篇关于在应用程序中过于频繁地使用非原始整数/长数据类型会损害性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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