fork 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. var s="1".repeat( 100160079 );
  22. ns=nanoTime(); lazy.matcher(s).matches(); out.println("Lazy String : "+ NANOSECONDS.toMillis(nanoTime()-ns));
  23. //ns=nanoTime(); greedy.matcher("1".repeat( 100160079 )).matches(); out.println("Greedy String:"+ NANOSECONDS.toMillis(nanoTime()-ns));
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30. static class Rep implements CharSequence
  31. {
  32. String str = null;
  33. int len;
  34. char base;
  35.  
  36. public Rep(char x, int count)
  37. {
  38. this.len = count;
  39. this.base = x;
  40. }
  41.  
  42. @Override
  43. public int length()
  44. {
  45. return len;
  46. }
  47.  
  48. @Override
  49. public char charAt(int index)
  50. {
  51. return base;
  52. }
  53.  
  54. @Override
  55. public CharSequence subSequence(int beginIndex, int endIndex)
  56. {
  57. if (beginIndex < 0) {
  58. throw new StringIndexOutOfBoundsException(beginIndex);
  59. }
  60. if (endIndex > this.len) {
  61. throw new StringIndexOutOfBoundsException(endIndex);
  62. }
  63. int subLen = endIndex - beginIndex;
  64. if (subLen < 0) {
  65. }
  66. return ((beginIndex == 0) && (endIndex == this.len)) ? this
  67. : new Rep(this.base, subLen);
  68. }
  69.  
  70. @Override
  71. public String toString()
  72. {
  73. return null!=str ? str : (this.str = new String(new char[]{base}).repeat(len));
  74. }
  75.  
  76.  
  77. }
  78. }
  79.  
Success #stdin #stdout 1.45s 136148KB
stdin
Standard input is empty
stdout
Lazy Rep    : 209
Lazy String : 1092