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

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

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

问题

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

示例:

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

或者

!"".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:

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

OR

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

答案1

得分: 3

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

!(true && false)
!(false)

=>True

但在另一种情况下

!true && !False 
False && True

=>False

英文:

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

!(true && false)
!(false)

=>True

But in other case

!true && !False 
False && True

=>False

答案2

得分: 2

以下是翻译好的内容:

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

这里有一个示例:

public static void main(String[] args){
    DecimalFormat df = new DecimalFormat("#.#########");
    String a = "ASD";
    String b = "XYZ";
    double sum = 0;
    double sum2 = 0;
    int nrOfIterations = 100_000_000;
    int nanoToSec = 1000_000_000;

    for(int i = 0; i < nrOfIterations; i++) {
        long start = System.nanoTime();
        if (!("".equals(a) && "".equals(b))) {
            long stop = System.nanoTime();
            double result = (stop - start) / (double) nanoToSec;
            sum += result;
        }

        long start2 = System.nanoTime();
        if (!"".equals(a) && !"".equals(b)) {
            long stop2 = System.nanoTime();
            double result2 = (stop2 - start2) / (double) nanoToSec;
            sum2 += result2;
        }
    }

    double average = sum / (double) nrOfIterations;
    String format = df.format(average);
    System.out.println(format + " sec");

    double average2 = sum2 / (double) nrOfIterations;
    String format2 = df.format(average2);
    System.out.println(format2 + " sec");
}

输出:

0.000000047 sec
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:

public static void main(String[] args){
DecimalFormat df = new DecimalFormat(&quot;#.#########&quot;);
String a = &quot;ASD&quot;;
String b = &quot;XYZ&quot;;
double sum = 0;
double sum2 = 0;
int nrOfIterations = 100_000_000;
int nanoToSec = 1000_000_000;
for(int i = 0; i &lt; nrOfIterations; i++) {
long start = System.nanoTime();
if (!(&quot;&quot;.equals(a) &amp;&amp; &quot;&quot;.equals(b))) {
long stop = System.nanoTime();
double result = (stop - start) / (double) nanoToSec;
sum += result;
}
long start2 = System.nanoTime();
if (!&quot;&quot;.equals(a) &amp;&amp; !&quot;&quot;.equals(b)) {
long stop2 = System.nanoTime();
double result2 = (stop2 - start2) / (double) nanoToSec;
sum2 += result2;
}
}
double average = sum / (double) nrOfIterations;
String format = df.format(average);
System.out.println(format + &quot; sec&quot;);
double average2 = sum2 / (double) nrOfIterations;
String format2 = df.format(average2);
System.out.println(format2 + &quot; sec&quot;);
}

Output:

0,000000047 sec
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:

确定