JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.stream.IntStream;
public class TestLoopSpeed
{
public static void main(String[] args) {
int n=1000000;
System.out.println("Speed of loop ("+n+" times):\n");
testForLoop(n);
testWhileLoop(n);
testIntStreamLoop(n);
testIntStreamParallelLoop(n);
}
public static void testForLoop(int n) {
long startTime = System.nanoTime();
for(int i=0;i<n;i++) ;
long endTime= System.nanoTime();
System.out.println("ForLoop time: "+(endTime -startTime)/1000000.00+" miliseconds\n");
}
public static void testWhileLoop(int n) {
long startTime = System.nanoTime();
int i=0;
while(i<n) {
i++;
}
long endTime= System.nanoTime();
System.out.println("WhileLoop time: "+(endTime -startTime)/1000000.00+" miliseconds\n");
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run