如果它只被使用两次,创建一个新的对象引用会更快吗? [英] Is it faster to create a new object reference if it will only be used twice?

查看:174
本文介绍了如果它只被使用两次,创建一个新的对象引用会更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于指令优化的问题。如果一个对象要在两个语句中使用,是否更快地创建一个新的对象引用,或者我应该直接在两个语句中调用对象?



为了目的对象是对象的一部分 Vector (这个例子来自Java没有ArrayLists的简化版本)。这里是一个例子:

  AutoEvent ptr =((AutoEvent)e_autoSequence.elementAt(currentEventIndex)); 
if(ptr.exitConditionMet()){currentEventIndex ++; return;}
ptr.registerSingleEvent();

AutoEvent e_autoSequence 是AutoEvent对象的 Vector AutoEvent 包含两个问题的方法: exitConditionMet() registerSingleEvent c $ c>。



因此,此代码可以写成:

  if(((AutoEvent)e_autoSequence.elementAt(currentEventIndex))。exitConditionMet())
{currentEventIndex ++; return;}
((AutoEvent)e_autoSequence.elementAt(currentEventIndex))。registerSingleEvent();

这比这更快吗?



我知道铸造过程很慢,所以这个问题实际上是双重的:另外,如果我不是铸造对象,这将更高度优化?



请注意,这只适用于两个对象的使用。

解决方案

第一个解决方案更全面:




  • 只有一个调用 c $ c> elementAt 方法。这实际上是这里最昂贵的操作,所以只做一次是一个体面的性能胜利。也可以两次潜在地打开你一些比赛条件。

  • 只有一个演员操作。铸件在现代JVM上非常便宜,但仍然有一点点成本。

  • 这是更易读 IMHO。你得到一个对象,然后做它的两件事。如果你得到了两次,那么读者必须精神上知道你得到了相同的对象。

  • (例如 ptr 在第一个解决方案中)非常便宜,通常是免费 - Java JIT编译器足够聪明,可以在这里产生高度优化的代码。



PS 矢量是相当过时的。考虑转换为 ArrayList< AutoEvent> 。通过使用通用的ArrayList,你不需要显式转换,它比一个 Vector 要快得多(因为它不是同步的,因此具有更少的锁定开销) / p>

I have a question about instruction optimization. If an object is to be used in two statements, is it faster to create a new object reference or should I instead call the object directly in both statements?

For the purposes of my question, the object is part of a Vector of objects (this example is from a streamlined version of Java without ArrayLists). Here is an example:

AutoEvent ptr = ((AutoEvent)e_autoSequence.elementAt(currentEventIndex));
if(ptr.exitConditionMet()) {currentEventIndex++; return;}
ptr.registerSingleEvent();

AutoEvent is the class in question, and e_autoSequence is the Vector of AutoEvent objects. The AutoEvent contains two methods in question: exitConditionMet() and registerSingleEvent().

This code could, therefore, alternately be written as:

if(((AutoEvent)e_autoSequence.elementAt(currentEventIndex)).exitConditionMet()) 
    {currentEventIndex++; return;}
((AutoEvent)e_autoSequence.elementAt(currentEventIndex)).registerSingleEvent();

Is this faster than the above?

I understand the casting process is slow, so this question is actually twofold: additionally, in the event that I am not casting the object, which would be more highly optimized?

Bear in mind this is solely for two uses of the object in question.

解决方案

The first solution is better all round:

  • Only one call to the vector elementAt method. This is actually the most expensive operation here, so only doing it once is a decent performance win. Also doing it twice potentially opens you up to some race conditions.
  • Only one cast operation. Casts are very cheap on moderns JVMs, but still have a slight cost.
  • It's more readable IMHO. You are getting an object then doing two things with it. If you get it twice, then the reader has to mentally figure out that you are getting the same object. Better to get it once, and assign it to a variable with a good name.
  • A single assignment of a local variable (like ptr in the first solution) is extremely cheap and often free - the Java JIT compiler is smart enough to produce highly optimised code here.

P.S. Vector is pretty outdated. Consider converting to an ArrayList<AutoEvent>. By using the generic ArrayList you won't need to explicitly cast, and it is much faster than a Vector (because it isn't synchronised and therefore has less locking overhead)

这篇关于如果它只被使用两次,创建一个新的对象引用会更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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