java 所需时间

发布于 2024-09-01 02:07:30 字数 242 浏览 2 评论 0原文

我想知道单独执行条件循环需要多少时间。 就像如果我可以选择使用“if else”、“while”、“for”或“foreach”循环,那么哪个循环会执行得更快。我知道差异会非常小,大多数人会说它不会很重要,但如果我有一个可以访问数千个数据的程序,那么这一点就会显现出来。

我想知道我是否在java的开头声明了一个变量,如果我在使用它之前声明它会有什么不同。所需的总时间会减少吗?如果是,那么实际使用的是哪个(在开始时声明变量的变量或刚刚声明它们的变量)?

i would like to know how much time is required to execute the conditional loops individually.
Like if i had an option to use "if else", "while","for", or "foreach" loop then which loop would get executed faster.I know the difference would be very small and most would say that it would not matter but if i were to have a program which would access thousands of data then this point would come into picture.

I would like to know if i declare a variable in the beginning in java and if i were to declare it just before i use it will it make any difference. will the total time required be reduced? if yes then which is practically used (the one in which variables are declared in the beginning or where they are just declared b4 they are used)?

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

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

发布评论

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

评论(8

始终不够 2024-09-08 02:07:30

停止微观优化。

花时间为您的系统寻找最佳算法、最佳数据结构和最佳设计,并编写最易读的代码。不是在这些小事上。

Stop micro-optimizing.

Spend your time on finding the best algorithms, the best data structures, and the best design for your system, and writing the most readable code. Not on these little things.

甜味超标? 2024-09-08 02:07:30

尝试手动优化您的代码并没有多大意义 - JIT 编译器可能会比您做得更好。尤其重要的是,它会比编译前拥有更多有关执行时的确切环境的信息。

此外,由于优化器是针对“常见情况”进行调整的,因此向代码中引入不常见的优化实际上可能会使其难以被 JIT 编译器优化,从而最终变慢。

尽管如此,如果您仍然确实想要测量某些东西,您可以这样做:

long before = System.currentTimeMillis();
// execute your code e.g. a million times
long after = System.currentTimeMillis();
System.out.println("Execution took " after - before " milliseconds");

There is not much point trying to hand-optimize your code - the JIT compiler is likely to do a lot better job than you. Not the least because it will have much more information about the exact environment at the point of execution than you can ever have before compilation.

Moreover, since optimizers are tuned for the "common case", introducing uncommon optimizations to your code may actually make it less optimizable by the JIT compiler, thus slower in the end.

Nevertheless, if you still really want to measure something, you can do it like this:

long before = System.currentTimeMillis();
// execute your code e.g. a million times
long after = System.currentTimeMillis();
System.out.println("Execution took " after - before " milliseconds");
余生共白头 2024-09-08 02:07:30

在一般情况下不可能回答,因为 Hotspot 可能会或可能不会对您的代码进行所有优化。

正如 Polygenelubricants 所说,重要的是编写清晰简洁的代码 - 这不仅会更容易让人理解(可以说是最重要的事情!),而且会更容易优化编译器“理解”并转化为高效的代码, 也。

如果您遇到性能问题,那么是时候破解一个分析器,查看您的代码实际上在哪里花费了时间并优化该部分。同样,这通常涉及改进算法,而不是使用略有不同的原语。

It's impossible to answer in the general case, because of all the optimisations that Hotspot may or may not make to your code.

The important thing, as polygenelubricants says, is to write clear concise code - not only will this be easier for humans to understand (arguably the most important thing!) but it will be easier for optimising compilers to "understand" and turn into efficient code, too.

if you're having performance problems, then it's time to crack out a profiler, see where your code is actually spending its time and optimise that section. Again, this will usually involve improving an algorithm rather than using a marginally different primitive.

温馨耳语 2024-09-08 02:07:30

如果它们完全相关的话,差异将非常小:-)

如今,编写清晰、可维护的代码比考虑 foreach 或 while 循环之间的差异要重要得多。

javac 编译器将优化生成的字节码,因此不应该有任何差异。您使用的算法也非常具体。

如果您在开头声明变量(?“开始”是什么),它可能会影响可见性,但可能不会影响应用程序的性能。

无论如何,如果您对此类性能优化感兴趣,请查看 Sun 的 Java VM 白皮书: http ://java.sun.com/docs/performance/

另外,您可能还想看看“分析”您的 Java 应用程序,以便您可以自己看到差异。以下是开源 Java 分析工具的列表: http://java-source.net/open -源/分析器

The difference will be very small, if they are relevant at all :-)

These days it is much more important to write clear, maintainable code than thinking of the difference between foreach or while loops.

The javac compiler will optimize the resulting bytecode, so there should not be any difference. Also its very specific which algorithms you use.

If you declare your variable in the beginning (? whats the "beginning") it will maybe have influence of the visibility, but likely not on the performance of your application.

Anyway, if you are interested in such performance optimization, have a look at the Java VM whitepapers from Sun: http://java.sun.com/docs/performance/

Also you maybe want to have a look at "profiling" your java applications, so you can see the difference by yourself. Here is a list of Open Source Java Profiling Tools: http://java-source.net/open-source/profilers

世界等同你 2024-09-08 02:07:30

让我们说得非常清楚。您在这里进行微观优化。这是浪费时间。以下是优化规则:

  1. 不要这样做。
  2. (仅限专家!)先不要这样做。

编写代码以使用好的算法。写代码要清楚。 检查您是否仍然遇到问题。 测量以找出问题实际存在的位置。仅在那时进行优化,并且仅在必须时。 (请注意,在现代计算机上获得良好的时序数据可能非常困难,特别是如果内存中有其他重要的东西(例如 IDE)。)

Let's be quite clear. You're micro-optimizing here. It's a waste of time. Here are the rules for optimization:

  1. Don't do it.
  2. (For experts only!) Don't do it yet.

Write to code to use good algorithms. Write the code to be clear. Check whether you still have a problem. Measure to find out where it is that the problem actually exists. Only optimize then, and only if you must. (And be aware that it can be exceptionally difficult to get good timing figures on modern computers, especially if you have non-trivial other things in memory such as an IDE.)

幸福丶如此 2024-09-08 02:07:30

差异将以微秒为单位,这意味着数千条记录意味着差异以毫秒为单位。

花时间思考为什么我首先需要读取/处理数千条记录?

The difference will be in microseconds, implies, thousands of records means difference in milliseconds.

Spend your time on thinking why I will need to read/process thousands of records in the first place?

欲拥i 2024-09-08 02:07:30

我同意@polygenelubricants 和@Péter Török 的观点。

不过,出于教育目的,您可以使用 javap 命令比较不同类型循环的编译代码:

$ javap -v YourClass

I agree with @polygenelubricants and @Péter Török.

For educational purposes though, you could compare the compiled code for the different types of loops using the javap command:

$ javap -v YourClass
家住魔仙堡 2024-09-08 02:07:30

你为什么不自己去发现呢?编写一些带有大循环的小程序并进行测量!

Why don't you find out yourself? Write a few smAll programs with large loops and do your measurements!

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