Two nested loops, each for 100000 times, in Java and C++. Have observed, with consistency, that Java outperforms C++. C++ took 3.26 s, whereas Java took 0.019 s. Can this be explained?
See the following two code snippets.
int main() {
for (int i = 0; i < 100000; i++) {
for (int j = 0; j < 100000; j++);
}
return 0;
}
class Main {
public static void main(String[] args) {
for (int i = 0; i < 100000; i++) {
for (int j = 0; j < 100000; j++);
}
}
}
First code is in C++ and the second one is Java. The first one is expected to be faster.
New contributor
Ajit Shukla is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1