문제

If I understand correctly Groovy is dynamically typed but since it's almost a superset of Java, static type information may optionally be provided. This could be useful if writing something where only a few parts are performance critical while avoiding the friction of using multiple languages. Type annotations could be provided only for the performance critical parts.

What is the performance penalty for using Groovy instead of Java in functions/classes where the Java-like subset is used and static type annotations are provided?

도움이 되었습니까?

해결책

Declaring types in groovy doesn't magically speed things up. It's still groovy code and needs to go through the MOP in case something got dynamically changed. You don't get static linking just because you've given type information.

For performance sensitive things that the groovy code just isn't fast enough for, you'll need to write real java code.

This question is very similar to a previous one where I gave an answer digging into the generated byte code showing how typing something doesn't speed things up.

다른 팁

You can also use Groovy++. Drop the groovypp.jar into the classpath, and annotate a package, class, or method with @Typed. The annotated code by-passes the MOP, therefore is virtually as fast as Java code.

And even if you don't declare a type, @Typed code will infer the type if possible at compile time.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top