[译] Kotlin VS Java:编译速度大比拼

原文链接https://medium.com/keepsafe-engineering/kotlin-vs-java-compilation-speed-e6c174b39b5d#.l8wax2t8j

译者:
昨天发表了一篇文章爽翻天!告别Java。一起来使用kotlin开发完整客户端 评论地下出现了一些不同的看法。这些看法、质疑都是好的,值得提倡的,因为只有这样,才可以进步,不过我觉得说一个东西不好的前提是有真正了解过,使用过,而不是在没有了解到情况下听信传言。也有人提出担心性能问题,所以找来国外一篇关于编译速度的文章。

正文:

把一个Java应用程序转换为Kotlin,编译时间要多久?

这是关于Kotlin的一系列文章。分为三个部分。 第一部分讨论了从Java转换到Kotlin。第二部分是我对Kotlin的看法。

在前面的文章中, 我讨论了把Android 应用从Java 100%转换为Kotlin 。 Kotlin代码比Java的简洁,更易于维护,所以我认为转换是值得的。 但有些人不想试用Kotlin,因为他们担心它编译可能没有Java快。 这个关注点绝对是正确的,如果变得编译很慢,没有人愿意转换他们的代码。 所以,让我们编译Lock App试一下 ,然后我把它转换成Kotlin。 我不会试图比较一行代码的编译速度; 相反,我将尝试回答将代码从Java转换为Kotlin是否会影响其总体构建的时间。

我如何测试构建时间

我写了一个shell来重复执行gradle。 所有测试连续进行10次。 该项目的每个场景之前clean,并使用Gradle daemon ,daemon之前停止一次。
本文中的所有测试都在运行于3.4 GHz的Intel Core i7-6700上,使用32GB的DDR4内存和三星850 Pro SSD。 源代码是用Gradle 2.14.1构建的。

测试

我想在几种常见的使用场景中运行基准:使用和不使用Gradle daemon+clean,没有文件更改的增量编译,以及更改的文件的增量编译。
在转换之前,App Lock的Java代码有5,491个方法和12,371行代码。 改写后,这些数字下降到4,987方法和8,564行Kotlin代码。 在重写期间没有发生大的架构更改,因此在重写之前和之后测试编译时间应该很好地了解Java和Kotlin之间的构建时间的差异。

clean + 不用Gradle daemon Build

这是两种语言中构建时间最差的情况:从冷启动运行一个clean的构建。 对于这个测试,我禁用了Gradle daemon。
这里是十个构建所花费的时间:

在这种情况下的结果是,Java构建时间平均为15.5秒,而Kotlin平均为18.5秒:增加了17%。 这对Kotlin来说并不是一个好的开始,但是大部分人不会这么编译他们的代码。

对于没有Gradle daemon 并且clean构建,Java编译比Kotlin快17%

clean +Gradle daemon Build

这个JIT编译器的问题 ,就像JVM中,是它们需要时间来编译对报告的执行的代码,等等的处理随时间增加的性能,因为它运行。 如果停止JVM进程,那么性能增益会丢失。 在构建Java代码时,通常在每次构建时启动和停止JVM。 这迫使JVM每次构建时重做工作。 为了解决这个问题,Gradle附带了一个守护进程,它将在构建之间保持活跃,以便保持JIT编译的性能提升。 你可以通过在gradle命令行加参数–daemon或者在gradle.properties文件添加一句org.gradle.daemon=true。

可以看到,第一次运行所花费的时间与没有daemon的时间相同,但后续运行的性能提高,直到第四次运行。 在这种情况下,查看第三次运行后的平均构建时间更有用,其中daemon已工作过了。 对于热运行,在Java中执行clean构建的平均时间为14.1秒,而Kotlin以16.5秒的速度运行时间:多了13%。

对于clean + Gralde daemon 编译,Java编译比Kotlin快13%。

Kotlin正在赶上Java,但仍然稍微落后。 但是,无论使用什么语言,Gradle daemon都会将构建时间减少40%以上。 如果你还没有使用它,你应该用上。

所以Kotlin编译在完整代码情况下比Java慢一点。 但是你通常只会对几个文件进行更改后编译,增量构建将有不同的性能。 所以,让我们来看看Kotlin在增量编译是否可以赶上。

增量构建

编译器最重要的性能特性之一是使用增量编译。 正常构建将重新编译项目中的所有源文件,但是增量构建将跟踪自上次构建以来哪些文件已更改,并且只重新编译这些文件和依赖它们的文件。 这可能对编译时间有巨大的影响,特别是对于大型项目。
增量构建在kotlin1.0.2以后版本支持 ,你可以在你的gradle.properties文件添加kotlin.incremental = true实现。
那么当使用增量编译时,Kotlin与Java的编译时相比如何? 以下是没有更改文件时使用增量编译的基准:

接下来,我们将使用修改后的源文件测试增量编译。 为了测试这个,我在每次构建之前改变了一个java文件,Kotlin也一样。 在这个基准测试中,源文件是没有其他文件依赖的UI文件:


最后,让我们看看使用修改的源文件进行增量编译,其中文件导入到项目中的许多其他文件

你可以看到Gradle daemon仍需要两三次运行来预热,但是之后两种语言的性能是非常相似的。 没有更改,Java每个热建立4.6秒,而Kotlin平均4.5秒。 当我们更改一个没有被任何其他文件使用的文件时,Java平均需要7.0秒来做一个热构建,Kotlin是6.1秒。 最后,当我们更改项目中许多其他文件导入的文件时,Java需要7.1秒才能在Gradle daemon加热后执行增量构建,而Kotlin平均6.0秒。

在最常见的情况下 – 启用增量编译的部分构建 – Kotlin编译速度快或略快于Java。

结论

我们对几个不同的场景进行了基准测试,看看Kotlin在编译时间是否可以跟上Java。 虽然Java在clean构建比Kotlin 快10-15%,但这些情况很少。 对于大多数开发人员来说,更常见的情况是部分构建,其中增量编译进行了大量改进。 随着Gradle daemon运行和增量编译的开启,Kotlin编译速度快或略快于Java。
这是一个我完全没有想到并且令人印象深刻的结果。 我必须赞扬Kotlin团队设计一种不仅具有很多优秀功能,而且能够快速编译的语言。
如果你因为编译时试图使用Kotlin,你不必担心:Kotlin的编译速度和Java一样快。

[译] Kotlin VS Java:编译速度大比拼》有89个想法

  1. Do you feel the pain of acid reflux? Do you feel a fire inside your chest? Are you miserable? Are you ready for the issues to stop? Continue reading to find out how. Keep reading to learn to control acid reflux for good and to end the misery for good.

    You may need to balance out hydrochloric acid amounts in your body if you want to reduce acid reflux and its symptoms. You can do this, for instance, by using sea salt rather than table salt. Sea salt has chloride and minerals that are good for the stomach and prevent acid.

    https://www.viagrasansordonnancefr.com/acheter-viagra-pharmacie/

  2. I simply want to tell you that I am just newbie to blogs and certainly enjoyed you’re blog site. Probably I’m want to bookmark your website . You certainly come with awesome articles and reviews. Cheers for sharing your webpage.

  3. Thank you so much for providing individuals with an extraordinarily nice chance to discover important secrets from this site. It is usually so superb and also stuffed with fun for me and my office co-workers to visit the blog more than 3 times per week to read through the latest issues you have. And definitely, I’m actually astounded for the splendid concepts you give. Certain two tips in this article are without a doubt the most efficient we’ve had.

  4. Needed to draft you this little bit of note in order to thank you so much over again on the precious tactics you have featured at this time. This has been quite seriously generous with people like you to offer without restraint all that many people might have distributed as an electronic book to get some money for their own end, and in particular considering that you might well have tried it if you wanted. The basics in addition served to be the great way to realize that some people have similar dream really like my personal own to know a lot more in regard to this condition. Certainly there are thousands of more fun times ahead for many who read carefully your site.

  5. I simply needed to say thanks yet again. I am not sure what I might have tried without those solutions shown by you relating to such a topic. It was the frightful scenario for me, however , observing your professional manner you treated the issue took me to weep over happiness. Now i’m thankful for the advice and as well , hope you really know what a powerful job you are always accomplishing teaching many others through a web site. I am certain you haven’t come across all of us.

  6. I want to point out my respect for your generosity for visitors who need help with this important niche. Your real dedication to getting the solution all around had been especially functional and have regularly helped employees just like me to realize their dreams. Your entire informative guideline indicates this much to me and even further to my office workers. Warm regards; from all of us.

  7. My spouse and i felt very glad Jordan managed to round up his web research while using the ideas he acquired out of your weblog. It is now and again perplexing to just choose to be freely giving strategies which many people have been trying to sell. We really figure out we’ve got the website owner to be grateful to because of that. The main illustrations you’ve made, the straightforward site menu, the friendships you can help foster – it’s got everything wonderful, and it is assisting our son in addition to the family feel that this subject is brilliant, and that’s unbelievably indispensable. Thank you for the whole thing!

  8. Thanks for all of the labor on this blog. Debby takes pleasure in working on investigations and it’s really easy to understand why. Most of us hear all regarding the lively tactic you make sensible information by means of your web site and therefore encourage participation from other ones on the topic while my child is certainly being taught so much. Take pleasure in the remaining portion of the new year. You’re performing a splendid job.

  9. I simply desire to reveal to you that I am new to blogging and thoroughly valued your webpage. More than likely I am inclined to remember your blog post . You really have stunning article material. Acknowledge it for expressing with us your own site information

  10. Hey this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

  11. The URL is needed to figure out the security zone, which is needed to ascertain the condition of the ActiveScripting setting. Completely ad-free, this website is quite easy to navigate and enables you to locate what you need fast. It’s a website that will permit you to really observe plenty of svg files created by very talented people at an affordable price. Everyone can join and add to the undertaking. This facility is known as SVG fonts.

  12. I must get across my respect for your kindness supporting men who really want assistance with the idea. Your very own commitment to getting the solution up and down has been particularly good and have constantly empowered those just like me to achieve their objectives. Your own valuable suggestions indicates so much a person like me and a whole lot more to my fellow workers. Thank you; from each one of us.

  13. It’s right occasion to generate some preparations for the near future. I’ve read through this blog post and if I may possibly, I want to propose you number of intriguing proposal.

  14. I simply want to show you that I am new to posting and clearly valued your information. Quite possibly I am likely to save your blog post . You absolutely have great article content. Truly Appreciate it for giving out with us your own website information

  15. After research a couple of of the weblog posts on your web site now, and I actually like your method of blogging. I bookmarked it to my bookmark web site record and will likely be checking back soon. Pls take a look at my web page as well and let me know what you think.

  16. I needed to create you this little word to finally say thanks a lot the moment again for your breathtaking tricks you’ve shared above. It is particularly open-handed of you to make without restraint just what a few people could possibly have advertised for an electronic book to generate some money for their own end, chiefly given that you might have tried it if you ever decided. These basics additionally acted to become easy way to recognize that most people have the same zeal much like my very own to figure out somewhat more when it comes to this matter. I believe there are numerous more enjoyable occasions up front for individuals that read your website.

  17. I simply needed to say thanks once again. I am not sure the things I would have accomplished in the absence of the type of concepts shared by you regarding that question. It became a real terrifying problem in my circumstances, but taking a look at a professional way you managed it forced me to weep over fulfillment. Extremely happy for this support and even wish you really know what an amazing job that you are doing educating people through the use of your websites. I’m certain you haven’t come across all of us.

  18. I am not sure where you’re getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for fantastic info I was looking for this information for my mission.

  19. Generally I do not read post on blogs, however I would like to say that this write-up very pressured me to try and do it! Your writing style has been amazed me. Thanks, very great article.

  20. I have not checked in here for some time since I thought it was getting boring, but the last few posts are great quality so I guess I¡¦ll add you back to my daily bloglist. You deserve it my friend 🙂

  21. Great goods from you, man. I’ve understand your stuff previous to and you are just too wonderful. I actually like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I cant wait to read much more from you. This is actually a great site.

  22. hello there and thank you for your information – I’ve definitely picked up anything new from right here. I did however expertise a few technical points using this web site, as I experienced to reload the site many times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading instances times will often affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my email and could look out for a lot more of your respective fascinating content. Ensure that you update this again very soon..

  23. Excellent post. I was checking constantly this blog and I am impressed! Extremely useful information specially the last part 🙂 I care for such info much. I was looking for this particular information for a long time. Thank you and good luck.

  24. I together with my pals happened to be analyzing the best helpful tips found on the blog and then all of a sudden got a terrible suspicion I never thanked the web site owner for those strategies. Most of the guys happened to be certainly glad to read them and already have extremely been enjoying them. Many thanks for actually being really thoughtful and also for using these kinds of very good subject matter most people are really eager to understand about. Our sincere regret for not expressing gratitude to sooner.

  25. I and also my pals were digesting the good thoughts located on the website and all of a sudden I had a terrible suspicion I had not expressed respect to the site owner for those secrets. Most of the ladies appeared to be thrilled to read through all of them and already have truly been loving those things. Thank you for getting so considerate and also for going for this form of exceptional information millions of individuals are really desperate to discover. My very own sincere regret for not saying thanks to you earlier.

  26. Thank you for every other informative website. The place else may just I get that type of info written in such a perfect approach? I’ve a venture that I’m simply now operating on, and I’ve been on the glance out for such information.

  27. I like the helpful information you provide in your articles. I’ll bookmark your weblog and check again here frequently. I’m quite sure I’ll learn a lot of new stuff right here! Best of luck for the next!

  28. Thank you so much for giving everyone an extremely special chance to read critical reviews from this site. It’s usually very fantastic and stuffed with fun for me and my office friends to visit your site no less than thrice per week to read the latest issues you will have. And lastly, I’m just at all times motivated considering the superb creative ideas served by you. Selected 3 tips on this page are easily the best I have ever had.

  29. Normally I don’t learn post on blogs, but I wish to say that this write-up very forced me to check out and do so! Your writing taste has been surprised me. Thank you, quite great post.

  30. It happens to be appropriate time to have some preparations for the upcoming. I’ve digested this article and if I can possibly, I wish to suggest you couple enlightening recommendation.

  31. My wife and i have been quite happy when Emmanuel managed to finish up his studies out of the ideas he got through your web site. It’s not at all simplistic just to always be giving freely ideas which usually the rest have been making money from. And now we see we have got the writer to appreciate because of that. Those explanations you’ve made, the straightforward blog menu, the relationships you help foster – it’s got everything wonderful, and it is letting our son and us believe that this subject is interesting, and that is exceedingly essential. Thank you for everything!

  32. I merely want to inform you that I am new to posting and really liked your webpage. Likely I am inclined to store your blog post . You seriously have amazing article content. Truly Appreciate it for expressing with us your favorite web document

  33. Needed to put you the very small note in order to say thanks as before for your personal marvelous information you have contributed on this page. It has been really pretty generous with you to make unhampered exactly what a few individuals could possibly have offered as an e-book to help with making some profit for their own end, notably seeing that you might well have done it if you decided. These tactics also worked to be a great way to know that many people have similar interest just like my own to see much more related to this matter. I’m sure there are millions of more pleasurable times in the future for individuals who go through your site.

  34. I must express appreciation to you for bailing me out of this type of dilemma. Right after looking out throughout the online world and obtaining notions which were not productive, I was thinking my entire life was gone. Living devoid of the approaches to the issues you’ve resolved all through your entire short post is a serious case, as well as those that would have in a wrong way affected my career if I hadn’t come across the website. Your own personal natural talent and kindness in dealing with all the details was valuable. I don’t know what I would’ve done if I hadn’t come upon such a solution like this. I can at this moment relish my future. Thanks very much for this reliable and result oriented help. I will not be reluctant to endorse the sites to any individual who should receive guide on this problem.

  35. I will immediately take hold of your rss feed as I can’t in finding your email subscription link or newsletter service. Do you’ve any? Kindly permit me understand so that I may just subscribe. Thanks.

  36. Great post. I was checking constantly this blog and I am impressed! Extremely helpful information particularly the last part 🙂 I care for such info much. I was looking for this particular info for a very long time. Thank you and best of luck.

  37. I really want to inform you you that I am new to writing and genuinely admired your report. Very likely I am probably to remember your blog post . You truly have impressive article information. Be Thankful For it for swapping with us your own website write-up

  38. hi!,I like your writing very so much! proportion we be in contact more approximately your post on AOL? I need a specialist on this area to resolve my problem. Maybe that’s you! Having a look ahead to see you.

  39. I am glad for commenting to make you understand of the perfect discovery our princess gained checking your blog. She mastered a good number of details, with the inclusion of how it is like to have an amazing teaching nature to let others completely know a variety of very confusing matters. You truly surpassed our expectations. Many thanks for displaying the interesting, safe, edifying and also fun guidance on the topic to Kate.

发表评论

电子邮件地址不会被公开。