Created
June 7, 2014 01:21
-
-
Save lucaspolo/18edf44c7252cea0c88e to your computer and use it in GitHub Desktop.
Teste com as Stream do Java 8 - Comum e Paralela
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Esta classe testa o Stream que é do Java 8 e seu irmão multi-thread ParallelStream | |
* Os resultados são controversos, algumas vezes o tempo é igual, outras o paralelo é mais rápido (metade do tempo). | |
* Ainda não verifiquei na documentação quais são os critérios. | |
**/ | |
public class Main { | |
public static void main(String[] args) { | |
List<Integer> lista = new ArrayList<>(); | |
System.out.println("Populando lista"); | |
for (int i = 0; i < 1_000_000; i++) { | |
lista.add(i); | |
} | |
System.out.println("Lista populada"); | |
long inicio = System.nanoTime(); | |
lista.stream().forEach(numero -> {}); | |
long fim = System.nanoTime(); | |
long primeiraExecucao = (fim - inicio) / 1000; | |
inicio = System.nanoTime(); | |
lista.stream().parallel().forEach(numero -> {}); | |
fim = System.nanoTime(); | |
long segundaExecucao = (fim - inicio) / 1000; | |
System.out.println(String.format("Tempo da primeira: %d", primeiraExecucao)); | |
System.out.println(String.format("Tempo da segunda: %d", segundaExecucao)); | |
} | |
} | |
//Populando lista | |
//Lista populada | |
//Tempo da primeira: 52677 | |
//Tempo da segunda: 22781 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment