fork(1) download
  1. import java.util.concurrent.TimeUnit;
  2. import java.util.regex.Pattern;
  3. import static java.lang.System.*;
  4. import static java.util.concurrent.TimeUnit.NANOSECONDS;
  5.  
  6.  
  7. public class Main
  8. {
  9. public static void main(String[] args)
  10. {
  11. long ns = 0L;
  12. Pattern lazy = Pattern.compile("^(11+?)\\1+$");
  13. Pattern greedy = Pattern.compile("^(11+)\\1+$" );
  14. ns=nanoTime(); lazy .matcher(new Rep('1',100160079)).matches(); out.println("Lazy Rep : "+ NANOSECONDS.toMillis(nanoTime()-ns));
  15. //ns=nanoTime(); greedy.matcher(new Rep('1',100160079)).matches();out.println("Greedy Rep :"+ NANOSECONDS.toMillis(nanoTime()-ns));
  16.  
  17. //ns=nanoTime(); "1".repeat( 100160079 ).matches("^(11+?)\\1+$") ; out.println("Lazy String:"+ NANOSECONDS.toMillis(nanoTime()-ns));
  18. //ns=nanoTime(); "1".repeat( 100160079 ).matches("^(11+)\\1+$") ; out.println("Greedy String:"+ NANOSECONDS.toMillis(nanoTime()-ns));
  19.  
  20. //no Pattern.compile()
  21. ns=nanoTime(); lazy.matcher("1".repeat( 100160079 )).matches(); out.println("Lazy String : "+ NANOSECONDS.toMillis(nanoTime()-ns));
  22. //ns=nanoTime(); greedy.matcher("1".repeat( 100160079 )).matches(); out.println("Greedy String:"+ NANOSECONDS.toMillis(nanoTime()-ns));
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29. static class Rep implements CharSequence
  30. {
  31. String str = null;
  32. int len;
  33. char base;
  34.  
  35. public Rep(char x, int count)
  36. {
  37. this.len = count;
  38. this.base = x;
  39. }
  40.  
  41. @Override
  42. public int length()
  43. {
  44. return len;
  45. }
  46.  
  47. @Override
  48. public char charAt(int index)
  49. {
  50. return base;
  51. }
  52.  
  53. @Override
  54. public CharSequence subSequence(int beginIndex, int endIndex)
  55. {
  56. if (beginIndex < 0) {
  57. throw new StringIndexOutOfBoundsException(beginIndex);
  58. }
  59. if (endIndex > this.len) {
  60. throw new StringIndexOutOfBoundsException(endIndex);
  61. }
  62. int subLen = endIndex - beginIndex;
  63. if (subLen < 0) {
  64. }
  65. return ((beginIndex == 0) && (endIndex == this.len)) ? this
  66. : new Rep(this.base, subLen);
  67. }
  68.  
  69. @Override
  70. public String toString()
  71. {
  72. return null!=str ? str : (this.str = new String(new char[]{base}).repeat(len));
  73. }
  74.  
  75.  
  76. }
  77. }
  78.  
Success #stdin #stdout 1.4s 136232KB
stdin
Standard input is empty
stdout
Lazy Rep    : 195
Lazy String : 1112