性能比较斯卡拉为Android [英] Perfomance of Scala for Android

查看:134
本文介绍了性能比较斯卡拉为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习斯卡拉和我有我的生活的时间。今天,我也只是听说斯卡拉为Android,而我完全上瘾了。

I just started learning Scala, and I'm having the time of my life. Today I also just heard about Scala for Android, and I'm totally hooked.

不过,考虑到Scala是一种像Java的超集,它就是Java ++(我指的是Java的更多的东西包括)我想知道究竟是怎么斯卡拉code将工作在Android的?

However, considering that Scala is kind of like a superset of Java in that it is Java++ (I mean Java with more stuff included) I am wondering exactly how the Scala code would work in Android?

此外,将Android应用程序的性能,如果要编写使用Scala的影响?也就是说,如果有需要跨preT斯卡拉code任何额外的工作。

Also, would the performance of an Android application be impacted if written with Scala? That is, if there's any extra work needed to interpret Scala code.

推荐答案

要解释一下@Aneesh的答案 - 是的,没有额外的工作来除preT斯卡拉字节code,因为它是完全一样的点点滴滴为Java字节code

To explain a little bit @Aneesh answer -- Yes, there is no extra work to interpret Scala bytecode, because it is exactly the same bits and pieces as Java bytecode.

请注意这里面还有一个Java字节code => 的Dalvik字节 $ C $当您运行code在Android的C步骤。

Note that there is also a Java bytecode => Dalvik bytecode step when you run code in Android.

但使用相同的砖块可以建立一个bikeshed和​​其他人可以建立一个市政厅。例如,由于这样的事实,语言鼓励不变性的Scala产生大量的短活对象。对于成熟的JVM像热点的是不是一个大问题了近十年。但Dalvik的这是一个问题(之前最近的版本对象池和紧重用已创建的对象是最常见的表现技巧之一甚至对Java)。

But using the same bricks one can build a bikeshed and the other guy can build a townhall. For example, due to the fact that language encourages immutability Scala generates a lot of short living objects. For mature JVMs like HotSpot it is not a big deal for about a decade. But for Dalvik it is a problem (prior to recent versions object pooling and tight reuse of already created objects was one of the most common performance tips even for Java).

接下来,编写VAL是不一样的写最后美孚吧= ... 。在内部,这个code重新psented作为一个字段+吸气剂($ P $,除非你$用点$ PFIX VAL私人[这] 将被翻译成通常最后一个字段)。 VAR 被翻译成现场+吸气+二传手。为什么重要?

Next, writing val isn't the same as writing final Foo bar = .... Internally, this code is represented as a field + getter (unless you prefix val with private [this] which will be translated to the usual final field). var is translated into field + getter + setter. Why is it important?

老版本没有JIT 可言,所以的this原来约3倍,7倍罚款相比,直接字段访问。最后,而谷歌指示您避免内部类和preFER包,而不是 ,斯卡拉将创造大量的内部类,即使你不这么写。考虑这个code:

Old versions of Android (prior to 2.2) had no JIT at all, so this turns out to about 3x-7x penalty compared to the direct field access. And finally, while Google instructs you to avoid inner classes and prefer packages instead, Scala will create a lot of inner classes even if you don't write so. Consider this code:

object Foo extends App {
    List(1,2,3,4)
      .map(x => x * 2)
      .filter(x => x % 3 == 0)
      .foreach(print)
}

有多少内部类将被创建?可以说没有,你要是跑不过scalac您将看到:

How many inner classes will be created? You could say none, but if you run scalac you will see:

Foo$$anonfun$1.class       // Map
Foo$$anonfun$2.class       // Filter
Foo$$anonfun$3.class       // Foreach
Foo$.class                 // Companion object class, because you've used `object`
Foo$delayedInit$body.class // Delayed init functionality that is used by App trait
Foo.class                  // Actual class

所以会有一些惩罚,特别是如果你写成语斯卡拉code有很多不变性和语法糖。问题是,这在很大程度上取决于您的部署(你的目标较新的设备?)和实际code模式(​​你总是可以回去Java或至少少写成语code的性能的关键点)以及一些的问题中提到会有由语言本身(最后一个)中的下一个版本中予以解决。

So there will be some penalty, especially if you write idiomatic Scala code with a lot of immutability and syntax sugar. The thing is that it highly depends on your deployment (do you target newer devices?) and actual code patterns (you always can go back to Java or at least write less idiomatic code in performance critical spots) and some of the problems mentioned there will be addressed by language itself (the last one) in the next versions.

原始图片来源是维基百科

又见堆栈溢出问题的 <一个href="http://stackoverflow.com/questions/3906532/is-using-scala-on-android-worth-it-is-there-a-lot-of-overhead-problems">Is使用Scala Android上值得吗?是否有一个很大的开销?问题? 的为您可能会在开发过程中遇到的可能出现的问题。

See also Stack Overflow question Is using Scala on Android worth it? Is there a lot of overhead? Problems? for possible problems that you may encounter during development.

这篇关于性能比较斯卡拉为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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