Which is more efficient for array lists: collections.swap() or using a temporary variable to swap?

huangapple go评论79阅读模式
英文:

Which is more efficient for array lists: collections.swap() or using a temporary variable to swap?

问题

Sure, here's the translation of the code:

int temp = name.get(0);
name.set(0, name.get(1));
name.set(1, temp);

Collections.swap(name, 0, 1);

我想要交换两个元素但不知道哪种方法更有效看起来这两种交换的运行时间是相同的但我不太确定谢谢
英文:
int temp = name.get(0);
name.set(0, name.get(1));
name.set(1, temp)

Collections.swap(name, 0, 1)

I want to swap two elements and don't know which is more efficient. It seems like the runtime of both swaps are the same but I'm not too sure. Thanks!

答案1

得分: 2

Collections.swap 是这样的:

public static void swap(List<?> list, int i, int j) {
    // 而不是在这里使用原始类型,可以捕获通配符,但这将需要调用一个附加的私有方法
    final List l = list;
    l.set(i, l.set(j, l.get(i)));
}

因此,有2个 get 和 2个 set,相比之下有1个 get 和 2个 set。此外,Collections.swap 很好地利用了 set 的返回值,以避免使用临时变量。

英文:

Collections.swap is:

public static void swap(List&lt;?&gt; list, int i, int j) {
        // instead of using a raw type here, it&#39;s possible to capture
        // the wildcard but it will require a call to a supplementary
        // private method
        final List l = list;
        l.set(i, l.set(j, l.get(i)));
}

So 2 gets and 2 sets vs. 1 get and 2 sets. Also Collections.swap nicely uses the return values from set to bypass the use of the temp variable.

答案2

得分: 1

以下是翻译好的内容:

> 我想要交换两个元素,但不知道哪种方法更有效。似乎两种交换的运行时间都相同,但我不太确定。

确定的方法是编写一个合适的微基准测试,运行它(在多个硬件平台/Java版本上),并解释结果。

我们可以查看源代码,然后做一些明智的猜测,但我们无法从第一原理推断微层次的效率<sup>1</sup>。

我的建议:

  • 您认为最易读的方式编写代码,让编译器进行优化。它们通常比大多数程序员做得更好。
  • 如果您关心应用程序的性能,那么编写一个应用程序基准测试,并使用分析工具找出真正的性能热点所在。
  • 利用热点信息决定在哪些地方值得花费精力来手动调优应用程序…而不是依靠您的直觉/猜测。

<sup>1 - ... 除非这里有人对现实世界中多个平台上的Java JIT编译器的工作原理拥有不健康程度的详尽知识。如果确实有这样的人在这里,我们或许应该让他们安静地休息,而不是用诸如这样的问题来打扰他们 :-)</sup>

英文:

> I want to swap two elements and don't know which is more efficient. It seems like the runtime of both swaps are the same but I'm not too sure.

The only way to be sure is to write a proper micro-benchmark, run it (on a number of hardware platforms / Java versions) and interpret the results.

We can look at the source code, and make some informed guesses, but we cannot deduce micro-level efficiency from first principles<sup>1</sup>.

My advice:

  • Write the code in the way that you think is most readable, and let the compilers do the optimization. They can typically do a better job than most programmers.
  • If performance of your application is a concern, then write an application benchmark and use a profiler to find out where the real performance hotspots are.
  • Use the hotspot information to decide where it is worthwhile expending effort in hand-tuning the application ... not your intuition / guesswork.

<sup>1 - ... unless there is someone here with an unhealthily detailed amount of knowledge in their heads about how real world Java JIT compilers actually work across multiple platforms. And if there is someone here like that, we should probably just let them rest quietly, rather than bugging them with questions like this :-)</sup>

huangapple
  • 本文由 发表于 2020年8月30日 13:24:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定