Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala vs F# on List range from 1 to 100000000

Tags:

list

scala

f#

I have written a list manipulation function both on F# and Scala to compare the performance. To test that function I need to initialize a List from 1 to 100000000.

F#:

let l = [1..100000000];;

Real: 00:00:32.954, CPU: 00:00:34.593, GC gen0: 1030, gen1: 520, gen2: 9

This works.

Scala: Scala -J-Xmx2G option

val l = (1 to 10000000).toList // works

val l = (1 to 100000000).toList // no response long while and finally got java.lang.OutOfMemoryError: Java heap space

With 100000000 (100,000,000), no response for a long while (an hour) with 75% to 90% CPU utilization and 2GB memory utilization and finally got java.lang.OutOfMemoryError: Java heap space.

Am I doing anything wrong in Scala?

like image 523
Sheik-Masha Avatar asked Oct 31 '14 11:10

Sheik-Masha


People also ask

Is it worth learning Scala in 2022?

Scala is worth learning in 2022. The demand for Scala developers is high, and the pay is good. LinkedIn currently lists over 24,000 Scala jobs. According to ZipRecruiter, the average Scala developer salary in the United States is $139,292 a year.

Is Scala really functional?

Scala is functional Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and it supports currying.

Why is F# not popular?

However, functional programming languages are not highly popular. Most programmers never learn any functional programming languages, choosing instead to learn one of C#, Java, C++, Javascript and stick with it indefinitely. For that reason, F# is stuck in a position of being the number 3 .

Is Scala in decline?

Scala shows a 0.2% decline in the PYPL index. The trends on Google and YouTube are also discouraging.


1 Answers

Note that val l = (1 to 100000000).toList creates a new List with contents from the original Range (1 to 100000000), hence a chance for a shortage in the heap space, as well as heavy triggering of the garbage collector. Increase -J-Xmx as @krynio suggested.

Yet without modifying the heap size, consider the use of iterators, especially if the performance test relies on a sequential iteration over the list; like this

(1 to 100000000).iterator
res0: Iterator[Int] = non-empty iterator
like image 56
elm Avatar answered Oct 24 '22 02:10

elm