fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println("- Removendo o primeiro item/indice");
  13. executarTeste("0");
  14. System.out.println();
  15. System.out.println("- Removendo um item/indice intermediario");
  16. executarTeste("5000");
  17. System.out.println();
  18. System.out.println("- Removendo o ultimo item/indice");
  19. executarTeste("9999");
  20. }
  21.  
  22. private static void executarTeste(String valueIndex) {
  23.  
  24. ArrayList<String> array1 = new ArrayList<>(10000);
  25. ArrayList<String> array2 = new ArrayList<>(10000);
  26.  
  27. for(int i = 0; i < 10000; i++){
  28. array1.add(i+"");
  29. array2.add(i+"");
  30. }
  31.  
  32. int indice = Integer.valueOf(valueIndex);
  33.  
  34. long startTime = System.nanoTime();
  35. array1.remove(indice);
  36. long endTime = System.nanoTime();
  37.  
  38. double duration1 = (endTime - startTime)/1000000d;
  39.  
  40. System.out.println("Tempo de remoção do remove(index)(ms): " + String.format("%.3f", duration1));
  41.  
  42. startTime = System.nanoTime();
  43. array2.remove(valueIndex);
  44. endTime = System.nanoTime();
  45.  
  46. double duration2 = (endTime - startTime)/1000000d;
  47.  
  48. System.out.println("Tempo de remoção do remove(Element)(ms): " + String.format("%.3f", duration2));
  49.  
  50. System.out.println("Diferença de tempo entre as duas formas: " + String.format("%.3f", (duration2-duration1)));
  51. }
  52. }
Success #stdin #stdout 0.21s 63120KB
stdin
Standard input is empty
stdout
- Removendo o primeiro item/indice
Tempo de remoção do remove(index)(ms): 0.014
Tempo de remoção do remove(Element)(ms): 0.018
Diferença de tempo entre as duas formas: 0.004

- Removendo um item/indice intermediario
Tempo de remoção do remove(index)(ms): 0.011
Tempo de remoção do remove(Element)(ms): 2.060
Diferença de tempo entre as duas formas: 2.049

- Removendo o ultimo item/indice
Tempo de remoção do remove(index)(ms): 0.004
Tempo de remoção do remove(Element)(ms): 0.445
Diferença de tempo entre as duas formas: 0.441