如果我也使用Java 7,我应该使用Groovy的@CompileStatic [英] Should I use Groovy's @CompileStatic if I'm also using Java 7

查看:704
本文介绍了如果我也使用Java 7,我应该使用Groovy的@CompileStatic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读Groovy 2.0中的新功能我对使用@CompileStatic时有点困惑。该文章提到,对于无法利用Java7的调用动态部分的开发人员,添加了 @CompileStatic 注释。


因此,寻找性能改进的开发人员如果不能在JDK 7上运行,Groovy 2.0将看不到太多变化。幸运的是,Groovy开发团队认为这些开发人员可以


我的问题是,如果我是的话,那么可以通过静态编译类型检查的代码来获得有趣的性能提升。使用JDK 7并按照说明添加 - indy 标志,是否需要将 @CompileStatic 添加到看到一些性能增加? 这个博客暗示我会,但我不确定他编译是否正确,因为他是在Eclipse中完成的。



更新:以下是运行不同排列的统计信息斐波那契代码。

 > groovy --indy FibBoth.groovy 
.......... Fib(非静态indy):1994.465
.......... Fib(静态indy): 529.197

> groovy FibBoth.groovy
.......... Fib(非静态):1212.788
.......... Fib(静态):525.671

注意:这个问题似乎有点令人困惑,因为我明白这些功能是独立的。由于这个问题的基础是来自笔记的混淆,使我认为这两个特征是相关的,所以我认为不要改变问题的措词并允许接受的解释差异的答案是有意义的。

$ b $因为众所周知,indy版本是完全动态的Groovy,由于JDK 7 invokedynamic的原因,其速度更快。 @CompileStatic表示静态编译​​。虽然比正常的Groovy快,但它只能编译Groovy的一个子集,而且行为有点不同。尤其是所有的动态功能都不再可用。所以如果你想使用动态功能,那么indy是唯一的选择。如果你是一个静态人,只使用一小部分语言,那么可以使用@CompileStatic。



斐波纳契不是一个invokedynamic可以闪耀的测试。 indy港并不总是更好。但是,如果你在元编程方面做了很多事情,那么indy将会更好。



你必须根据你的使用情况决定最终的结果。 $ b

I've read through the "What's new in Groovy 2.0" and I'm a bit confused about when to use @CompileStatic. The article mentions that the @CompileStatic annotation was added for developers who weren't able to take advantage of the invoke dynamic part of Java7.

So developers looking for performance improvements would not see much changes in Groovy 2.0, if they aren’t able to run on JDK 7. Luckily, the Groovy development team thought those developers could get interesting performance boost, among other advantages, by allowing type checked code to be compiled statically.

My question is, if I'm using JDK 7 and I follow the instructions to add the --indy flag, do I need to add @CompileStatic to see some performance increases? This blog suggests I would, but I'm not sure he compiled correctly given that he did it within Eclipse.

Update: Here are the stats when running different permutations of the Fibonacci code.

> groovy --indy FibBoth.groovy
..........Fib (non-static indy): 1994.465
..........Fib (static indy): 529.197

> groovy FibBoth.groovy       
..........Fib (non-static): 1212.788
..........Fib (static): 525.671

Note: this question seems a little confusing now that I understand that the features are independent. Since the basis of the question is around the confusion from the notes that made me think the two features were related I think it makes sense not to change the question wording and allow the accepted answer that explain the differences.

解决方案

The indy version is fully dynamic Groovy, as it is known, only faster thanks to JDK 7 invokedynamic. @CompileStatic means static compilation. While faster than normal Groovy, it can compile only a subset of Groovy and behaves a bit different. Especially all the dynamic features are not available anymore. So if you want to use dynamic features then indy is the only option. If you will be a static guy and only use a small part of the language, then @CompileStatic can be used.

Fibonacci is btw not a test in which invokedynamic can shine. The indy port is not always better. But if you do for example a lot with meta programming, then indy will be better.

You have to decide according to your usage in the end

这篇关于如果我也使用Java 7,我应该使用Groovy的@CompileStatic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆