Scala vs Java,性能和内存? [英] scala vs java, performance and memory?

查看:500
本文介绍了Scala vs Java,性能和内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我热衷于研究Scala,并提出了一个基本问题,但我似乎无法找到答案: 通常,Scala和Java之间在性能和内存使用方面是否有所不同?

I am keen to look into Scala, and have one basic question I cant seem to find an answer to: in general, is there a difference in performance and usage of memory between Scala and Java?

推荐答案

Scala可以非常轻松地使用大量内存而无需意识到.这通常非常强大,但有时可能很烦人.例如,假设您有一个字符串数组(称为array),以及从这些字符串到文件的映射(称为mapping).假设您要获取映射中所有来自长度大于2的字符串的文件.在Java中,您可能

Scala makes it very easy to use enormous amounts of memory without realizing it. This is usually very powerful, but occasionally can be annoying. For example, suppose you have an array of strings (called array), and a map from those strings to files (called mapping). Suppose you want to get all files that are in the map and come from strings of length greater than two. In Java, you might

int n = 0;
for (String s: array) {
  if (s.length > 2 && mapping.containsKey(s)) n++;
}
String[] bigEnough = new String[n];
n = 0;
for (String s: array) {
  if (s.length <= 2) continue;
  bigEnough[n++] = map.get(s);
}

哇!辛苦了在Scala中,做同一件事的最紧凑的方法是:

Whew! Hard work. In Scala, the most compact way to do the same thing is:

val bigEnough = array.filter(_.length > 2).flatMap(mapping.get)

容易!但是,除非您非常熟悉集合的工作方式,否则您可能不会意识到,这种方式创建了一个额外的中间数组(使用filter),并为该对象的每个元素创建了一个额外的对象.数组(带有mapping.get,返回一个选项).它还创建了两个函数对象(一个用于过滤器,一个用于flatMap),尽管由于函数对象很小,所以这很少成为主要问题.

Easy! But, unless you're fairly familiar with how the collections work, what you might not realize is that this way of doing this created an extra intermediate array (with filter), and an extra object for every element of the array (with mapping.get, which returns an option). It also creates two function objects (one for the filter and one for the flatMap), though that is rarely a major issue since function objects are small.

因此,基本上,内存使用量在原始级别上是相同的.但是Scala的库具有许多强大的方法,可让您轻松地创建大量(通常是短暂的)对象.垃圾收集器通常可以很好地处理这种垃圾,但是如果您完全不了解正在使用的内存,那么Scala可能会比Java更快地遇到麻烦.

So basically, the memory usage is, at a primitive level, the same. But Scala's libraries have many powerful methods that let you create enormous numbers of (usually short-lived) objects very easily. The garbage collector is usually pretty good with that kind of garbage, but if you go in completely oblivious to what memory is being used, you'll probably run into trouble sooner in Scala than Java.

请注意,计算机语言基准游戏Scala代码以类似于Java的风格编写,以便获得类似于Java的性能,因此具有类似于Java的内存使用量.您可以在Scala中做到这一点:如果您编写的代码看起来像高性能的Java代码,那么它将是高性能的Scala代码. (您也许可以用更惯用的Scala风格编写它,但仍然可以获得良好的性能,但这取决于具体情况.)

Note that the Computer Languages Benchmark Game Scala code is written in a rather Java-like style in order to get Java-like performance, and thus has Java-like memory usage. You can do this in Scala: if you write your code to look like high-performance Java code, it will be high-performance Scala code. (You may be able to write it in a more idiomatic Scala style and still get good performance, but it depends on the specifics.)

我应该补充一点,因为每花费编程时间,我的Scala代码通常比Java代码更快,因为在Scala中,我可以用更少的精力完成乏味的,不影响性能的部分,并花费我更多的精力来优化性能关键部件的算法和代码.

I should add that per amount of time spent programming, my Scala code is usually faster than my Java code since in Scala I can get the tedious not-performance-critical parts done with less effort, and spend more of my attention optimizing the algorithms and code for the performance-critical parts.

这篇关于Scala vs Java,性能和内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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