Java,最昂贵的语句?

发布于 2024-08-03 16:22:32 字数 44 浏览 1 评论 0原文

Java 编程语言中最昂贵(无论是字节码还是 CPU 周期)的语句是什么?

What are the most expensive (both in terms of bytecode and cpu cycles) statements in the Java Programming language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清君侧 2024-08-10 16:22:32

在任何语言中,您都可以通过多次执行各种语句并查看它们需要多长时间来了解它们的速度。我想你的问题比这更明智。例如,在这种情况下,许多调整步骤执行了,并且在每一步中,主要问题都是其他问题。

  • 第一步:主要的时间消耗者相当于递增迭代器。 (修复?使用整数索引。)

  • 修复该问题后,问题是一次构建一个元素的链表。 (修复?一次性构建它们。)

  • 重新设计后,主要问题是为对象分配和释放存储空间。 (修复?重新使用使用过的对象。)

在每个阶段,都有一些问题是最大的。解决这个问题(并获得良好的加速)后,其他一些问题成为新的最大问题。解决这个问题后......(依此类推,直到你能看到最大的问题是什么,但你无法解决它)。

你看,它几乎与“哪些语句最昂贵”无关。如果某个声明正在做您绝对需要完成的事情,并且您找不到更好的方法来完成它,那么根据定义,它是最适合该工作的声明。

In any language, you can find out the speed of various statements by doing them lots of times and seeing how long they take. I'm supposing your question is more intelligent than that. For example, in this scenario, A number of tuning steps were performed, and at each step, something else was the major problem.

  • First step: major time-taker was the equivalent of incrementing iterators. (Fix? Use integer indexes.)

  • After fixing that, the problem was building linked lists one element at a time. (Fix? Build them all at once.)

  • After a redesign, the major problem was allocating and freeing storage for objects. (Fix? Re-use used objects.)

At each stage, some problem is the largest. After fixing that (and getting a good speedup), some other problem is the new largest. After fixing that... (and so on, until you can see what the largest problem is, but you can't fix it).

You see, it has almost nothing to do with "which statements are most expensive". If a statement is doing something that you absolutely need done, and you can't find a better way to do it, then by definition it is the best statement for the job.

雪落纷纷 2024-08-10 16:22:32

这没有一个简单的答案。这取决于您的虚拟机和底层硬件。您使用的虚拟机可能会将字节代码编译为机器代码,因此问题是:硬件上最昂贵的汇编调用是什么。

传统上最糟糕的是计算方面的除法。但现在计算成本很低,而且内存也很遥远。因此,现代硬件上最昂贵的调用是错过缓存的内存访问。访问主内存需要 500-1000 个 CPU 周期。

诸如以下的行:

   x++;

因此,如果 x 已被推出缓存,则 ...可能需要数百个周期。最常见的例子是遍历内存中的链表。

for (ListElement n = ...; n != null; n = n.next()) {
  n.val++;
}

这里每次调用n.next()可能需要500-1000个周期,因为链表可以稀疏地分布在系统内存中。

This does not have an easy answer. It depends on your VM and on your underlying hardware. The VM you are using probably compiles your byte code to machine code, so the question would be: what is the most expensive assembly call on your hardware.

Classically the worst was divide in terms of computation. But computation is cheap these days, and memory is far away. So the most expensive calls on modern hardware are memory accesses that miss the cache. Going out to main memory costs 500-1000 CPU cycles.

So a line such as:

   x++;

...might take hundreds of cycles, if x has been pushed out the cache. The most common example of this is traversing a linked list that is in memory.

for (ListElement n = ...; n != null; n = n.next()) {
  n.val++;
}

Here the call n.next() can take 500-1000 cycles each time, because the linked list can be sparsely distributed in system memory.

私野 2024-08-10 16:22:32

可能,至少对于 CPU 周期,异常处理。

Possibly, at least for cpu cycles, exception handling.

迷爱 2024-08-10 16:22:32

我敢打赌 sleep() 一定是成本最高的之一! ;)

您的问题需要更多细节。一个人总是可以写

while(true) {}

,它会无休止地消耗CPU周期。您在谈论什么类型的陈述?

任何涉及 IO 的内容或将序列化代码的内容,例如调用 System.out。

I would bet that sleep() has to be one of the costliest! ;)

Your question needs more details. One can always write

while(true) {}

and it will burn up CPU cycles endlessly. What types of statements are you talking about?

Anything that involves IO or something that will serialize your code like calls to System.out.

若沐 2024-08-10 16:22:32

在CPU周期方面:

public class Zombie extends Thread {
    public void run() {
        while (true)
        {(new Zombie()).start();}
    }

    public static void main(String args[]) {
        (new Zombie()).start();
    }
}

从这里借用并改编,尽管不可否认,这不完全是一个单一的陈述。

In terms of CPU cycles:

public class Zombie extends Thread {
    public void run() {
        while (true)
        {(new Zombie()).start();}
    }

    public static void main(String args[]) {
        (new Zombie()).start();
    }
}

Borrowed and adapted from here, though admittedly not exactly a single statement.

旧街凉风 2024-08-10 16:22:32

OP,看看 BCEL 项目,您可能会感兴趣,了解有关的具体信息Java 字节码。

OP, take a look at the BCEL project, it may be of interest to you, for learning the specifics about Java ByteCode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文