Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory allocation with views

Tags:

memory

julia

Consider the following code

using Distributions
using BenchmarkTools

u = randn(100, 2)
res = ones(100)
idx = 1
u_vector = u[:, idx]

@btime $res = $1.0 .- $u_vector;
@btime $res = $1.0 .- $u[:,idx];
@btime @views $res = $1.0 .- $u[:,idx];

These are the results that I got from the three lines with @btime

julia> @btime $res = $1.0 .- $u_vector;
  37.478 ns (1 allocation: 896 bytes)

julia> @btime $res = $1.0 .- $u[:,idx];
  607.383 ns (13 allocations: 1.97 KiB)

julia> @btime @views $res = $1.0 .- $u[:,idx];
  397.597 ns (6 allocations: 1.08 KiB)

The second @btime line has the greatest amount of time and allocations but that's in line with my expectation, since I'm slicing. However, I'm not sure why the third line with @views is not the same as the first line? I thought by using @views I'm not longer creating a copy. Is there a way to "fix" the third line? In my real code, the user provides idx so idx is not known in advance. Therefore, I would want to reduce allocations when I do slicing.

like image 288
user1691278 Avatar asked Sep 15 '26 17:09

user1691278


1 Answers

What I assume you are looking for is:

julia> @btime $res .= 1.0 .- view($u, :,$idx);
  13.126 ns (0 allocations: 0 bytes)

The point is that you want to avoid allocation of the vector on RHS, and that is why you should use .= not =.

I also changed @views to view call. It does not matter here, but, in general using @views is tricky at times, and I avoid it unless there is a reason, see here.

like image 62
Bogumił Kamiński Avatar answered Sep 22 '26 10:09

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!