fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Random;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. private static Random rnd = new Random();
  12.  
  13. public static void testeComList(int quantidade){
  14. List lista = new ArrayList(quantidade);
  15.  
  16. long inicio = System.currentTimeMillis();
  17.  
  18. for (int i = 0; i < quantidade; i++){
  19. lista.add(i);
  20. }
  21.  
  22. Collections.shuffle(lista);
  23.  
  24. long fim = System.currentTimeMillis();
  25.  
  26. System.out.println("Shuffle: Tempo com " + quantidade + " elementos = " + (fim - inicio));
  27. }
  28.  
  29. public static void testeComSet(int quantidade){
  30.  
  31. Set<Integer> set = new LinkedHashSet<>();
  32.  
  33. long inicio = System.currentTimeMillis();
  34.  
  35. while (set.size() < quantidade) {
  36. set.add(rnd.nextInt(quantidade));
  37. }
  38.  
  39. long fim = System.currentTimeMillis();
  40.  
  41. System.out.println("Set: Tempo com " + quantidade + " elementos = " + (fim - inicio));
  42.  
  43. }
  44.  
  45.  
  46. public static void main (String[] args) throws java.lang.Exception
  47. {
  48.  
  49. testeComList(20);
  50.  
  51. testeComList(20000);
  52.  
  53. testeComList(200000);
  54.  
  55. testeComList(20000000);
  56.  
  57. testeComSet(20);
  58.  
  59. testeComSet(20000);
  60.  
  61. testeComSet(200000);
  62.  
  63. testeComSet(20000000);
  64.  
  65. }
  66. }
Time limit exceeded #stdin #stdout 5s 2317312KB
stdin
Standard input is empty
stdout
Shuffle: Tempo com 20 elementos = 0
Shuffle: Tempo com 20000 elementos = 14
Shuffle: Tempo com 200000 elementos = 28
Shuffle: Tempo com 20000000 elementos = 2002
Set: Tempo com 20 elementos = 0
Set: Tempo com 20000 elementos = 42
Set: Tempo com 200000 elementos = 202