简体   繁体   English

与Java相比,Groovy的性能如何?

[英]How is the performance of Groovy compared with Java?

与Java相比,Groovy的性能如何?

It's obviously true that Groovy is compiled to JVM. Groovy显然已编译为JVM。 This however has little to do with the performance. 但是,这与性能无关。

The most important thing to note here is that Groovy is a dynamic language. 这里要注意的最重要的一点是Groovy是一种动态语言。 This essentially means that most of the time Groovy compiler will have little to no knowledge about the type of an object it is calling a method on / retrieving a property from. 从本质上讲,这意味着大部分时间,Groovy编译器对于在其上调用方法或从中检索属性的对象的类型几乎一无所知。 This has a huge impact on the performance. 这对性能有巨大影响。 There might be thousands of different classes implementing someFancyMethodName() not having a common base class. 可能有成千上万个不同的类实现someFancyMethodName()而不具有公共基类。 Yet a call to obj.someFancyMethodName() has to choose the right one. 但是,对obj.someFancyMethodName()的调用必须选择正确的对象。 There isn't any better way of doing this than deciding it at runtime based on some kind of reflection. 没有什么比在运行时基于某种反射来决定更好的方法了。 In fact, because of this every single call to a method gets dispatched through a call to invokeMethod() on the object metaclass. 实际上,由于这个原因,对方法的每个单独调用都通过对对象元类的invokeMethod()的调用而分派。 This is very much visible in stacktraces if your program ever throws some nasty exceptions. 如果您的程序曾经抛出一些令人讨厌的异常,则这在堆栈跟踪中非常明显。 It's even worse. 更糟的是。 Any class in groovy may choose to provide implementations of methods of the given name dynamically, that is producing them at runtime. groovy中的任何类都可以选择动态地提供给定名称的方法的实现,即在运行时生成它们。 There is a fair amount of Grails magic that makes a heavy use of it. 有大量的Grails魔法会大量使用它。 Another complication emerges when method overloading comes into play. 当方法重载起作用时,还会出现另一种复杂情况。 As the knowledge of types is so limited, it's impossible to choose the right version of the method at compile time. 由于类型的知识非常有限,因此无法在编译时选择正确的方法版本。 The produced code has to look into the supplied objects and then by making a series of if-elses choose the implementation that best fits the provided call. 产生的代码必须查看提供的对象,然后通过执行一系列if-els选择最适合提供的调用的实现。 This most of the time is a really non-trivial process, that was never intended to be performed at runtime. 大多数时候,这是一个非常重要的过程,从来没有打算在运行时执行。 Yet, Groovy has to do it, in order to stay inter-operable with Java. 但是,为了保持与Java的互操作性,Groovy必须这样做。

All that makes Groovy pretty slow. 所有这些使Groovy变得非常慢。 In fact much slower and, what is more painful, more memory consuming than most of the dynamic languages out there (Python for instance). 实际上,与目前大多数动态语言(例如Python)相比,它要慢得多,而且更痛苦,需要更多的内存。

That said, I agree that the reason for using Groovy is certainly not performance. 也就是说,我同意使用Groovy的原因当然不是性能。 Most of the time, you will end up optimizing only a small fraction of your code. 大多数时候,您最终只会优化一小部分代码。 If performance is such an issue, you can always resort to rewriting those specific pieces in pure Java or give a try to Groovy++. 如果性能是一个问题,那么您总是可以使用纯Java重写这些特定的部分,或者尝试使用Groovy ++。 Haven't tried it myself, however the results that I read about online seemed pretty promising. 我自己还没有尝试过,但是我在网上阅读的结果似乎很有希望。

Groovy 2.0 I have no experience in running the newer version. Groovy 2.0我没有运行较新版本的经验。 Quite frankly, I'm not an active Groovy user anymore. 坦率地说,我不再是Groovy的活跃用户。 I would however expect that most of the issues described above, are fundamentally hard and require a major scientific breakthrough. 但是,我希望上述大多数问题在根本上都是棘手的,需要重大的科学突破。 I have some experience developing HHVM (a PHP virtual machine created by Facebook) and there are much simpler features that performed poorly. 我有一些开发HHVM(由Facebook创建的PHP虚拟机)的经验,并且有许多简单的功能却表现不佳。

So here we are in 2012 and Groovy 2.0 is ready to rock... 所以我们到了2012年,Groovy 2.0即将动摇...

"With the @CompileStatic, the performance of Groovy is about 1-2 times slower than Java, and without Groovy, it's about 3-5 times slower. (...) This means to me that Groovy is ready for applications where performance has to be somewhat comparable to Java." “使用@ CompileStatic,Groovy的性能大约比Java慢1-2倍,而如果没有Groovy,它的速度则要慢3-5倍。(...)对我来说,这意味着Groovy已为具有性能的应用程序做好了准备。可以和Java相提并论。”

Performance Test: Groovy 2.0 vs. Java http://java.dzone.com/articles/groovy-20-performance-compared 性能测试:Groovy 2.0与Java http://java.dzone.com/articles/groovy-20-performance-compared

And besides the autor, I've used Groovy since 2008 with great success, not only for CV, just to make job done in time business need. 除了授课老师之外,我从2008年开始就使用Groovy取得了巨大的成功,不仅对于简历,而且是为了及时完成业务需求。 Performance is ever relative to what you want to do. 性能永远与您想要做的事情有关。


For those who are complaining about numeric use cases, here goes a real use case using web frameworks: http://www.jtict.com/blog/rails-wicket-grails-play-lift-jsp/ 对于那些抱怨数值用例的人,这里有一个使用Web框架的真实用例: http : //www.jtict.com/blog/rails-wicket-grails-play-lift-jsp/


"Groovy 1.8.x prototype for fib(42) takes about 3.8s ( only 12% slower than Java, over a hundred times faster than Groovy 1.0 ) So we may no longer encourage people to write such 'hot spots' in Java." “ fib(42)的Groovy 1.8.x原型大约需要3.8s( 仅比Java慢12%,比Groovy 1.0快100倍以上 ),因此我们可能不再鼓励人们在Java中编写这样的'热点'。”

Source: http://www.wiki.jvmlangsummit.com/images/0/04/Theodorou-Faster-Groovy-1.8.pdf 资料来源: http : //www.wiki.jvmlangsummit.com/images/0/04/Theodorou-Faster-Groovy-1.8.pdf

"I'm impressed on how much Groovy's performance has improved for numerical computing. Groovy 1.8 in my project jlab (http://code.google.com/p/jlabgroovy/) sometimes outperforms Scala's performance in my other project ScalaLab (http://code.google.com/p/scalalab) !!" “对于数字计算,Groovy的性能提高了很多,给我留下了深刻的印象。我的项目jlab(http://code.google.com/p/jlabgroovy/)中的Groovy 1.8 有时在我的其他项目ScalaLab(http: //code.google.com/p/scalalab)!!“

Source: http://groovy.329449.n5.nabble.com/Great-improvements-in-Groovy-s-performance-for-numerical-computing-td4334768.html 资料来源: http : //groovy.329449.n5.nabble.com/Great-improvements-in-Groovy-s-performance-for-numerical-computing-td4334768.html

Groovy offers a lot more syntactic sugar over Java, but still runs on the JVM and therefore requires a bit more work by the JVM to provide that sugar. Groovy提供了比Java更多的语法糖,但仍在JVM上运行,因此JVM需要更多的工作来提供糖。 Nevertheless, the difference is extremely minor in the vast majority of normal usages. 但是,在绝大多数的正常用法中,差别很小。

In addition, if you do happen to write a function that runs too slowly in Groovy, you can write it in straight Java and call it from your Groovy code. 另外,如果您确实碰巧编写了一个在Groovy中运行太慢的函数,则可以用纯Java编写它并从Groovy代码中调用它。 That's the team's recommended solution, and I can vouch for it working well and simply. 那是团队推荐的解决方案,我可以保证它运作良好且简单。

It my opinion, for the programming most of us do, it's a non-issue. 我认为,对于我们大多数人所做的编程而言,这不是问题。

A quick Google search yielded some old performance results ( http://www.codecommit.com/blog/java/groovys-performance-is-not-subjective , http://www.christianschenk.org/blog/performance-comparison-between-groovy-and-java/ ). 快速谷歌搜索产生了一些旧的性能结果( http://www.codecommit.com/blog/java/groovys-performance-is-not-subjectivehttp://www.christianschenk.org/blog/performance-comparison- groovy和Java之间 )。

Groovy++ looks interesting also ( http://stronglytypedblog.blogspot.com/2010/02/java-vs-scala-vs-groovy-vs-groovy.html ). Groovy ++看起来也很有趣( http://stronglytypedblog.blogspot.com/2010/02/java-vs-scala-vs-groovy-vs-groovy.html )。

However, the reason to use Groovy should be because it improves your performance not the computers... 但是,使用Groovy的原因应该是因为它提高了您的性能, 而不是计算机。

I think , you have to look at this scientific comparison of Groovy Vs Python Vs PHP vs Ruby. 我认为,您必须查看Groovy与Python与PHP与Ruby的科学对比。

http://blog.websitesframeworks.com/2013/11/comparison-of-programming-languages-ruby-groovy-python-and-php-353/ http://blog.websitesframeworks.com/2013/11/comparison-of-programming-languages-ruby-groovy-python-and-php-353/

They have made one exercise and produce comparison on these programming languages on the below factors: 他们进行了一项练习,并就以下因素对这些编程语言进行了比较:

Comparison of time developing each exercise

Comparison of readability of the languages

Comparison of results in benchmarks and lines of code. From the project Computer Language Benchmarks Game

Conclusions

It is a great quick study to enable you which language is better. 这是一项很棒的快速学习,可以使您了解哪种语言更好。

Generally speaking, Groovy will be slower. 一般来说,Groovy会变慢。 You can avoid that by switching to Groovy++ which offers most of the features of Groovy, but can be statically compiled and has performance comparable to Java. 您可以通过切换到Groovy ++来避免这种情况,它具有Groovy的大多数功能,但可以静态编译并且具有与Java相当的性能。

While developing AWS Lambdas Java will be faster than groovy. 在开发AWS Lambdas时,Java将比常规速度更快。 Apart from that in all other scenarios you might not feel much difference. 除此之外,在其他所有情况下,您可能不会感到太大差异。

Groovy被编译为字节码.class文件,但是运行Groovy类需要大约5MB的groovy库,这会增加性能开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM