Scala vs F # listed Range from 1 to 100,000,000

I wrote a list manipulation function in both F # and Scala to compare performance. To test this 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

It 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 100,000,000 (100,000,000) unanswered for a long time (hour) from 75% to 90% of the processor load and 2 GB memory usage and finally java.lang.OutOfMemoryError: Java heap space.

Am I doing something wrong in Scala?

+6
source share
1 answer

Note that val l = (1 to 100000000).toList creates a new List with contents from the original Range (1 to 100000000) , therefore, the probability of running out of space on the heap, as well as the intensive launch of the garbage collector. Increase -J-Xmx as @krynio suggested.

However, without changing the heap size, consider using iterators, especially if the performance test is based on sequential list iteration; like this

 (1 to 100000000).iterator res0: Iterator[Int] = non-empty iterator 
+5
source

Source: https://habr.com/ru/post/977535/


All Articles