I have a code below. It prints time of List filling. When circles = 1000000 the output is 300 ms When circles = 1000000*10 the output is around 7000 ms. But intuitively I've expected that it should be 300 * 10. Scaladoc says adding element to List is O(1). Why does it behave like that?
val random = new Random()
var ints: List[Int] = List()
val start = System.currentTimeMillis();
val circles = 1000000
for (i <- 1 to circles) {
ints = random.nextInt.signum :: ints
}
println(System.currentTimeMillis() - start);
if I use constant instead of random the results are 156 ms and 5300 for circles = 1000000 and circles = 1000000*10