Why need to pay more time when access the array at first time?

I run the code below, and find we need more times when manipulate the array at the first time. I want to know why, and can we avoid it?

function test()
	a = Vector{ComplexF64}(undef, 2^25)
	@time fill!(a, 0.0+0.0im)
	@time fill!(a, 1.0+0.0im)
    @time fill!(a, 2.0+0.0im)
    println()
end

function loop()
	test()
	test()
	test()
	test()
	test()
	test()
	return
end
loop()

Results as below

  0.363740 seconds
  0.066602 seconds
  0.072507 seconds

  0.352474 seconds
  0.062322 seconds
  0.068734 seconds

  0.351361 seconds
  0.062778 seconds
  0.071476 seconds

  0.349806 seconds
  0.064757 seconds
  0.067769 seconds

  0.349292 seconds
  0.054079 seconds
  0.057817 seconds

  0.346038 seconds
  0.053347 seconds
  0.059138 seconds

This is happening because the first time you touch the memory it is being brought into your CPUs cache. Julia isn’t doing anything different here.

15 Likes

Oh, that makes sense. Thank you!

This may also depend on your operating system. Some operating systems are lazy about actually allocating pages of memory until you write to those pages.

3 Likes

Yes, I run the code in WSL and get the results above.
While it’s so different when I run it in windows REPL, the results is

  0.069340 seconds
  0.047052 seconds
  0.046383 seconds

  0.069447 seconds
  0.046711 seconds
  0.047232 seconds

  0.069558 seconds
  0.049158 seconds
  0.047343 seconds

  0.070144 seconds
  0.046805 seconds
  0.046005 seconds

  0.070537 seconds
  0.047274 seconds
  0.046166 seconds

  0.069857 seconds
  0.046681 seconds
  0.046450 seconds

Is this reasonable? Why WSL has to pay much longer time?

Time the line @time a = Vector{ComplexF64}(undef, 2^25) as well and try your tests again, maybe that’ll show what previous comments are describing.

Here is the results, changed to @time a = Vector{ComplexF64}(undef, 2^25) to measure allocation time.

For WSL, the results is

  0.031786 seconds (2 allocations: 512.000 MiB, 99.88% gc time)
  0.336840 seconds
  0.048195 seconds
  0.046878 seconds

  0.031919 seconds (2 allocations: 512.000 MiB, 99.84% gc time)
  0.335299 seconds
  0.048593 seconds
  0.048650 seconds

  0.032231 seconds (2 allocations: 512.000 MiB, 99.80% gc time)
  0.336596 seconds
  0.049577 seconds
  0.048941 seconds

  0.034741 seconds (2 allocations: 512.000 MiB, 99.87% gc time)
  0.334516 seconds
  0.049514 seconds
  0.049246 seconds

  0.031316 seconds (2 allocations: 512.000 MiB, 99.88% gc time)
  0.335624 seconds
  0.049551 seconds
  0.048465 seconds

  0.034916 seconds (2 allocations: 512.000 MiB, 99.85% gc time)
  0.334452 seconds
  0.048894 seconds
  0.048763 seconds

And for REPL in windows, the results is

  0.041377 seconds (2 allocations: 512.000 MiB, 99.94% gc time)
  0.074445 seconds
  0.046818 seconds
  0.046252 seconds

  0.040182 seconds (2 allocations: 512.000 MiB, 99.86% gc time)
  0.074390 seconds
  0.048710 seconds
  0.047181 seconds

  0.039944 seconds (2 allocations: 512.000 MiB, 99.94% gc time)
  0.075219 seconds
  0.047892 seconds
  0.046741 seconds

  0.039637 seconds (2 allocations: 512.000 MiB, 99.94% gc time)
  0.074268 seconds
  0.046461 seconds
  0.046737 seconds

  0.039268 seconds (2 allocations: 512.000 MiB, 99.93% gc time)
  0.074602 seconds
  0.047076 seconds
  0.048861 seconds

  0.039869 seconds (2 allocations: 512.000 MiB, 99.94% gc time)
  0.074913 seconds
  0.048400 seconds
  0.048094 seconds

It seems that WSL cost much more times at first time. Is this because WSL is a virtual machine?

WSL2 or WSL1?

1 Like

NAME STATE VERSION

  • Ubuntu Running 1