Is Java-style Groovy as fast as Java?
Asked Answered
F

2

13

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?

Finalist answered 5/9, 2011 at 0:39 Comment(0)
G
12

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.

Guarnerius answered 5/9, 2011 at 1:1 Comment(1)
I believe this answer is out-of-date now that @CompileStatic has been added to the main Groovy language.Bettinabettine
D
8

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.

Deltoro answered 6/9, 2011 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.