英文:
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("#.#########");
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");
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论