-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
45 lines (40 loc) · 1.6 KB
/
Main.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
List<Integer> sizes = new ArrayList<Integer> (Arrays.asList(200, 2000, 20000, 200000, 2000000, 20000000));
Random random = new Random();
check1(sizes, random);
System.out.println("Without streams");
check2(sizes, random);
}
private static void check1(List<Integer> sizes, Random random) {
sizes.forEach(size -> {
long startTime = System.currentTimeMillis();
Integer[] array = new Integer[size];
Arrays.fill(array, random.nextInt());
final int[] count2 = {0};
Arrays.stream(array).forEach(item -> {
if(item ==2) count2[0]+=1;
});
long endTime = System.currentTimeMillis();
System.out.println("Array size: "+ size +" time for simple count operation: " + (endTime - startTime) +" in ms");
});
}
private static void check2(List<Integer> sizes, Random random) {
sizes.forEach(size -> {
long startTime = System.currentTimeMillis();
Integer[] array = new Integer[size];
Arrays.fill(array, random.nextInt());
int count2 = 0;
for (int item: array){
if(item ==2) count2+=1;
}
long endTime = System.currentTimeMillis();
System.out.println("Array size: "+ size +" time for simple count operation: " + (endTime - startTime) +" in ms");
});
}
}