Which statement would compile fast? !(condition1 && condition2) or !condition1 && !condition2

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

Which statement would compile fast? !(condition1 && condition2) or !condition1 && !condition2

问题

我知道这只是微不足道的时间差异,但是我对此产生了一些好奇,因为在我的项目中有超过5个条件检查的if循环。我知道对于第二个条件,当condition1变为false时,jvm会跳过循环,但我会在这里发布它,以确定我是对还是错,以及其他任何想法。

示例:

  1. !("".equals(ASD) && "".equals(XYZ))

或者

  1. !"".equals(ASD) && !"".equals(XYZ)
英文:

I know that this is negligible amount of time difference but I got some curiosity as I had more than 5 conditions checking on if loop for my project. I know that for the second one as condition1 becomes false the jvm skips the loop but posting it here if I am wrong or right and any other thoughts.

Example:

  1. !("".equals(ASD) && "".equals(XYZ))

OR

  1. !"".equals(ASD) && !"".equals(XYZ)

答案1

得分: 3

这两者有不同的意义。
让我们考虑 condition1->true 和 condition2->false

  1. !(true && false)
  2. !(false)

=>True

但在另一种情况下

  1. !true && !False
  2. False && True

=>False

英文:

These both have different meanings.
lets take condition1->true and condition2->false

  1. !(true && false)
  2. !(false)

=>True

But in other case

  1. !true && !False
  2. False && True

=>False

答案2

得分: 2

以下是翻译好的内容:

奥姆卡尔·阿罗拉提出了一个很好的观点。条件虽然不同,但如果你想测试哪个更快,你可以创建一个测试平台。

这里有一个示例:

  1. public static void main(String[] args){
  2. DecimalFormat df = new DecimalFormat("#.#########");
  3. String a = "ASD";
  4. String b = "XYZ";
  5. double sum = 0;
  6. double sum2 = 0;
  7. int nrOfIterations = 100_000_000;
  8. int nanoToSec = 1000_000_000;
  9. for(int i = 0; i < nrOfIterations; i++) {
  10. long start = System.nanoTime();
  11. if (!("".equals(a) && "".equals(b))) {
  12. long stop = System.nanoTime();
  13. double result = (stop - start) / (double) nanoToSec;
  14. sum += result;
  15. }
  16. long start2 = System.nanoTime();
  17. if (!"".equals(a) && !"".equals(b)) {
  18. long stop2 = System.nanoTime();
  19. double result2 = (stop2 - start2) / (double) nanoToSec;
  20. sum2 += result2;
  21. }
  22. }
  23. double average = sum / (double) nrOfIterations;
  24. String format = df.format(average);
  25. System.out.println(format + " sec");
  26. double average2 = sum2 / (double) nrOfIterations;
  27. String format2 = df.format(average2);
  28. System.out.println(format2 + " sec");
  29. }

输出:

  1. 0.000000047 sec
  2. 0.000000048 sec

关于输出的评论:

经过 100,000,000 次迭代,这些条件的表现几乎相同。
更多的迭代会给出更精确的性能平均值。

英文:

Omkar Arora is making a good point. The conditions are not the same, but if you want to test which one is the fastest then you can make a test bench.

Here is an example:

  1. public static void main(String[] args){
  2. DecimalFormat df = new DecimalFormat(&quot;#.#########&quot;);
  3. String a = &quot;ASD&quot;;
  4. String b = &quot;XYZ&quot;;
  5. double sum = 0;
  6. double sum2 = 0;
  7. int nrOfIterations = 100_000_000;
  8. int nanoToSec = 1000_000_000;
  9. for(int i = 0; i &lt; nrOfIterations; i++) {
  10. long start = System.nanoTime();
  11. if (!(&quot;&quot;.equals(a) &amp;&amp; &quot;&quot;.equals(b))) {
  12. long stop = System.nanoTime();
  13. double result = (stop - start) / (double) nanoToSec;
  14. sum += result;
  15. }
  16. long start2 = System.nanoTime();
  17. if (!&quot;&quot;.equals(a) &amp;&amp; !&quot;&quot;.equals(b)) {
  18. long stop2 = System.nanoTime();
  19. double result2 = (stop2 - start2) / (double) nanoToSec;
  20. sum2 += result2;
  21. }
  22. }
  23. double average = sum / (double) nrOfIterations;
  24. String format = df.format(average);
  25. System.out.println(format + &quot; sec&quot;);
  26. double average2 = sum2 / (double) nrOfIterations;
  27. String format2 = df.format(average2);
  28. System.out.println(format2 + &quot; sec&quot;);
  29. }

Output:

  1. 0,000000047 sec
  2. 0,000000048 sec

Comment on output:

After 100_000_000 iterations the conditions performed pretty much the same.
More iterations will give a more precised average of the performance.

huangapple
  • 本文由 发表于 2020年8月25日 14:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63573501.html
匿名

发表评论

匿名网友

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

确定