-4

consider the below code

while(100 times){
Pattern abc = Pattern.compile("[0-9]+");
}

Does java compile the pattern once or 100 times ?

Lakshminathan
  • 116
  • 1
  • 8
  • You might want to look at the generated bytecode to see how often the method is getting called. I'm guessing it is called 100 times, there is nothing special about that method, the java compiler does not know every method of the jdk and wether or not it has side effects and can be optimized away. – luk2302 Jun 28 '17 at 06:31
  • I don't see why a good compiler _couldn't_ optimize this away, but it would take work to write a compiler that could do this kind of analysis. So my answer would be that it depends on the compiler, but a _free_ compiler would probably call `Pattern.compile` 100 times, because an organization that puts in the significant extra effort to optimize things like this would likely want to be paid for their effort. – ajb Jun 28 '17 at 06:51
  • See also: https://stackoverflow.com/questions/13420321/does-pattern-compile-cache – Stephen C Jun 28 '17 at 07:31
  • @ajb - AFAIK, no existing free or non-free compiler will "hoist" the pattern compilation out if the loop. – Stephen C Jun 28 '17 at 07:33
  • 1
    [`java.util.Scanner` does](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Scanner.java#363). – Holger Jun 28 '17 at 17:12
  • @Holger I don't get what you mean by this. `java.util.Scanner` does not read Java source and generate byte code. `java.util.Scanner` apparently does have Java code of its own to avoid redundant calls to `Pattern.compile`, but the question isn't remotely about that. – ajb Jun 29 '17 at 04:03
  • I disagree with marking this as a duplicate. Maybe it's a matter of interpretation, but the question looks to me like a question about what the compiler does, not a question about what the implementation of `Pattern.compile()` does. Note that the two are _very_ different. If the compiler hoists the call out of the loop, then there is no additional overhead. If the runtime caches it, it introduces the overhead of doing a cache lookup and maintaining the cache. – ajb Jun 29 '17 at 04:06
  • @ajb: maybe you are just interpreting the word “compile” differently. The conversion of the pattern string to something executable is called “compile” too, as the method name `Pattern.compile` suggests. This question does not mandate any responsibility in the “compiled only once” scenario. It could be `javac` generating optimized code, it could be the `Pattern.compile` implementation maintaining a cache or the JVM replacing `Pattern.compile(String)` invocation with a constant result if the argument is a constant string, it doesn’t matter. None of this happens today. Only `Scanner` has a cache. – Holger Jun 29 '17 at 07:46

1 Answers1

2

JIT might optimize it away at least in theory, but even then it would require more than 100 loops.

The displayed code is just poor code, since the pattern doesn't change and can easily be reused by declaring it outside of the loop. Of course compiling 100 Patterns is still so fast that you wouldn't really see a difference.

The javac compiler will not attempt to do anything, as it optimizes only very simple things.

Kayaman
  • 72,141
  • 5
  • 83
  • 121