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. //------- sample data
  13. List<String> list = new ArrayList<String>();
  14. for(int i=0; i<1000000; i++) {
  15. list.add("LABEL ...");
  16. }
  17.  
  18. //------- starts with
  19. long start = System.nanoTime();
  20. for(String record : list) {
  21. if(record.startsWith("LABEL")) System.out.print("");
  22. }
  23. long end = System.nanoTime();
  24. long diffStartsWith = end-start;
  25.  
  26. //------- regex
  27. start = System.nanoTime();
  28. for(String record : list) {
  29. if(record.matches("LABEL.*")) System.out.print("");
  30. }
  31. end = System.nanoTime();
  32. long diffRegex = end-start;
  33.  
  34. //------- results
  35. System.out.printf("StartsWith took %f seconds.%n", Math.pow(10, -9)*(diffStartsWith));
  36. System.out.printf("Regex took %f seconds.%n", Math.pow(10, -9)*(diffRegex));
  37. }
  38. }
Success #stdin #stdout 2.54s 380608KB
stdin
Standard input is empty
stdout
StartsWith took 0.170032 seconds.
Regex took 2.237698 seconds.