使用enum.values()和String数组时,是否有性能问题? [英] is there a performance hit when using enum.values() vs. String arrays?

查看:111
本文介绍了使用enum.values()和String数组时,是否有性能问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用枚举来替换我的java应用程序(JRE 1.5)中的 String 常量。



是当我将枚举作为静态名称的静态数组(例如渲染UI)时,会将枚举当作一个性能命中?



我的代码看起来有点这样:

  public String getValue(int col){
return ColumnValues.values()[col] .toString ();
}



澄清:




  • 我关心的是与枚举 values()相关的隐藏成本(例如paint()方法)

  • 我现在可以看到,我的所有场景包括一些 int => 枚举转换 - 这是不是Java的方式。



提取 values()的实际价格是什么()数组?这是一个问题吗?



Android开发人员



阅读Simon Langhoff的回答,在接受回答的评论中,怪胎在拥抱。 Enum.values() 必须做防御副本

解决方案

Enum.values()提供了对数组的引用,并且迭代枚举数组的成本与迭代字符串数组相同。同时,将枚举值与其他枚举值进行比较,实际上可以更快地比较字符串与字符串。



同时,如果您担心调用 values()方法的成本与已经具有数组引用的费用不用担心。 Java中的方法调用(现在)非常快,任何时候它对于性能来说都是至关重要的,所以方法调用将被编译器内联。



不要担心。改为代替可读性,并使用枚举,以便编译器可以捕获它,如果您尝试使用您的代码不希望处理的常数值。






如果您对于枚举比较可能更快的原因感到好奇比较字符串比较,这里有详细信息:



这取决于字符串是否是 interned 。对于枚举对象,系统中始终只有一个枚举值的一个实例,因此每个调用 Enum.equals()可以非常快地完成,就好像你使用的是 == 运算符,而不是 equals()方法。实际上,使用枚举对象,可以使用 == 而不是 equals() ,而可以安全地使用字符串。



对于字符串,如果字符串被实体,那么该比较与$ code>枚举一样快。然而,如果这些字符串没有被实体,那么 String.equals()方法实际上需要走两个字符串中的字符列表,直到任一个字符串结束或它发现两个字符串之间的字符不同。但是再一次,这可能并不重要,即使在Swing渲染代码中,必须快速执行。 : - )






@Ben Lings指出 Enum.values()必须做一个防御性的副本,因为数组是可变的,并且可以替换由 Enum.values()返回的数组中的值。这意味着您必须考虑该防御副本的成本。然而,复制单个连续的数组通常是一种快速的操作,假设它是使用某种内存复制调用实现的,而不是天真地迭代数组中的元素。所以,我不认为这是最后的答案。


I'm using enumerations to replace String constants in my java app (JRE 1.5).

Is there a performance hit when I treat the enum as a static array of names in a method that is called constantly (e.g. when rendering the UI)?

My code looks a bit like this:

public String getValue(int col) {
  return ColumnValues.values()[col].toString();
}

Clarifications:

  • I'm concerned with a hidden cost related to enumerating values() repeatedly (e.g. inside paint() methods).
  • I can now see that all my scenarios include some int => enum conversion - which is not Java's way.

What is the actual price of extracting the values() array? Is it even an issue?

Android developers

Read Simon Langhoff's answer below, which has pointed out earlier by Geeks On Hugs in the accepted answer's comments. Enum.values() must do a defensive copy

解决方案

Enum.values() gives you a reference to an array, and iterating over an array of enums costs the same as iterating over an array of strings. Meanwhile, comparing enum values to other enum values can actually be faster that comparing strings to strings.

Meanwhile, if you're worried about the cost of invoking the values() method versus already having a reference to the array, don't worry. Method invocation in Java is (now) blazingly fast, and any time it actually matters to performance, the method invocation will be inlined by the compiler anyway.

So, seriously, don't worry about it. Concentrate on code readability instead, and use Enum so that the compiler will catch it if you ever try to use a constant value that your code wasn't expecting to handle.


If you're curious about why enum comparisons might be faster than string comparisons, here are the details:

It depends on whether the strings have been interned or not. For Enum objects, there is always only one instance of each enum value in the system, and so each call to Enum.equals() can be done very quickly, just as if you were using the == operator instead of the equals() method. In fact, with Enum objects, it's safe to use == instead of equals(), whereas that's not safe to do with strings.

For strings, if the strings have been interned, then the comparison is just as fast as with an Enum. However, if the strings have not been interned, then the String.equals() method actually needs to walk the list of characters in both strings until either one of the strings ends or it discovers a character that is different between the two strings.

But again, this likely doesn't matter, even in Swing rendering code that must execute quickly. :-)


@Ben Lings points out that Enum.values() must do a defensive copy, since arrays are mutable and it's possible you could replace a value in the array that is returned by Enum.values(). This means that you do have to consider the cost of that defensive copy. However, copying a single contiguous array is generally a fast operation, assuming that it is implemented "under the hood" using some kind of memory-copy call, rather than naively iterating over the elements in the array. So, I don't think that changes the final answer here.

这篇关于使用enum.values()和String数组时,是否有性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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