![]() This would result in the following code: function A = up(v)Ī(i,j) = v(j) % copy the value from the vector to the matrix Given the way that you are using the loops, it may be preferable to use for loops instead of while loops. You also will not get any return value as the variable Upp is not assigned. Then you can write Julia which is pretty close to optimal in terms of performance without going too far down the rabbit hole.As I said in my comment, you are seeing the output because of the missing semicolon on the line a(i,j) = v(i, j). Avoid accidentally creating lots of copies.Pre-allocate your outputs when possible.Overall, though, I think the message is that if you: I won’t go into detail on that here because it’s been covered in a lot of other posts on this forum, but you can dig as deep as you’re interested. There’s more you can do, like using to avoid bounds checks, using to vectorize your innermost loop, and using StaticArrays.jl to represent your data as a collection of small vectors rather than a matrix. Very slightly faster, but the return on effort is diminishing at this point. We can avoid that by constructing a matrix with undef, which tells Julia to just allocate memory but not fill it with anything: function distance_matrix4(c)Ĭdist = Matrix(undef, size(c, 1), triangle_number(size(c, 2) - 1)) That means we’re wasting time filling the initial array with zeros. Next, you might notice that we’re creating cdist as a matrix of zeros and then setting every element. The difference will be larger for big matrices due to cache locality. We can make the operations a bit faster, especially on large matrices, if you’re willing to decide to operate over columns instead of rows: function distance_matrix3(c)Ĭdist = zeros(eltype(c), size(c, 1), triangle_number(size(c, 2) - 1)) Note that this is similar to Matlab and Fortran but different than Numpy and C for boring historical reasons. That’s because matrices in Julia are stored in column-major layout, so each column is contiguous in memory. Iterating over the rows of a matrix in Julia is, in general, slower than iterating over the columns. The next issue I see with the code is that the inner-most loop is over each row of the matrix c. Of course we can’t just stop here, because it wouldn’t be Julia if we didn’t try to make it perfect. ![]() Okay, almost 20x faster, and using 20x less memory. Let’s check the result: julia> distance_matrix2(c) = distance_matrix(c)Īnd see how it performs: julia> distance_matrix2($c) # depends on the `n - 1`th triangle number, where n is the numberĬdist = zeros(eltype(c), triangle_number(size(c, 1) - 1), size(c, 2)) ![]() # (I learned this algorithm from a wonderful book called "The Number Devil" One easy way to get really good performance in this case is to pre-allocate the entire result matrix and then write out the loop: # Returns the nth triangle number, i.e.
0 Comments
Leave a Reply. |