import java.util.*;
public class TestaPerformance {
public static void main(String[] args) {
Collection<Integer> arrayListOfNumeros = new ArrayList<Integer>();
Collection<Integer> hashSetOfNumeros = new HashSet<>();
buscaUsandoArrayList(arrayListOfNumeros);
buscaUsandoHashSet(hashSetOfNumeros);
}
private static void buscaUsandoArrayList(Collection<Integer> arrayListOfNumeros) {
long inicio = System.currentTimeMillis();
for (int i = 1; i <= 50000; i++) {
arrayListOfNumeros.add(i);
}
for (Integer numero : arrayListOfNumeros) {
arrayListOfNumeros.contains(numero);
}
long fim = System.currentTimeMillis();
long tempoDeExecucao = fim - inicio;
System.out.println("Tempo gasto com ArrayList: " + tempoDeExecucao);
}
private static void buscaUsandoHashSet(Collection<Integer> hashSetOfNumeros) {
long inicio = System.currentTimeMillis();
for (int i = 1; i <= 50000; i++) {
hashSetOfNumeros.add(i);
}
for (Integer numero : hashSetOfNumeros) {
hashSetOfNumeros.contains(numero);
}
long fim = System.currentTimeMillis();
long tempoDeExecucao = fim - inicio;
System.out.println("Tempo gasto com HashSet: " + tempoDeExecucao);
}
}