6

I would like to understand what shall I put instead of xxxxx to have a 1000 items array initialized with structs where a goes from 2000 to 3000 (i.e., index 1 of the array means a is 2000, index 2 of the array means a is 2001, and so on) and b is always zero.

struct MyStruct
    a
    b
end

myArray = Vector{MyStruct}( xxxxx , 1000)

I understand I can make a loop and individually assign the values, I am just wondering if there is something faster in Julia.

1
  • 1
    Keep in mind that you should specify types for the field members of your struct, otherwise it will be very slow. It you need it to be able to hold different types, make it parametric.
    – DNF
    Sep 16, 2019 at 10:08

2 Answers 2

8

Just collecting the answers and comments from other users in a single post:

No, there is nothing faster than a loop in Julia

Unlike other scripting languages like Python and R, loops are fast in Julia. In fact, other "vectorized" operations, like broadcasting, are implemented in terms of Julia loops themselves. Thus, a fast solution could be:

function initialize_vector(range::AbstractRange)
    v = Vector{MyStruct}(undef, length(range))
    @inbounds for i in eachindex(range)
        v[i] = MyStruct(range[i], 0)
    end
    return v
end

Broadcasting is both fast and convenient

Broadcasting is almost, or sometimes just as fast as looping, and can often be more terse and convenient. In this case, the function initialize_vector above can be written:

initialize_vector(range::AbstractRange) = MyStruct.(range, 0)

Benchmarking shows that the two functions are almost the same in speed.

Remember to type the fields in your struct for faster code

Julia relies on accurate inference of types to create fast, specialized code. If the types of MyStruct.a and MyStruct.b can be anything, it's generally not possible to infer exactly what kind of operations should be performed on a MyStruct. Even in this case, where the compiler is able to infer that the types are Int, each MyStruct has to contain references to heap-allocated Ints instead of being stack-allocated. Thus, a 10x speedup is obtained from simply changing

struct MyStruct
    a
    b
end

to

struct MyStruct
    a::Int
    b::Int
end

If you want to the type of Mystruct.a and MyStruct.b to be able to vary, you can create a parametric MyStruct, like so:

struct MyStruct{T}
    a::T
    b::T
end
2
  • If MyStruct has a third element, name it c which is a String, would it make this faster if the String is fixed length? Is that possible in Julia? If so, how?
    – M.E.
    Sep 18, 2019 at 6:03
  • @M.E. Sorry for only seeing your comment now. Yes, a fixed-length string would make your struct faster, as long as the string is short. You cannot make fixed-length strings in Julia. However, you can make your own fixed-length struct which behaves like a string. There is probably some package implementing that, but I can't find any. If your strings are very short (8 bytes or less), I would just use an UInt64 as backing storage, and if it's larger (up to say, 100 bytes), I'd use a SArray{Tuple{3},UInt8,1,3} from the package StaticArrays. Oct 28, 2019 at 9:07
3

Broadcasting via the . works nicely here. You could also provide a vector/collection for b:

struct MyStruct
    a
    b
end

struct_vec = [MyStruct.(2000:3000, 0)...]
struct_vec2 = [MyStruct.(2000:3000, 0:1000)...]

Which results in a 1001-element Array{MyStruct,1}.

Edit As mentioned in the comments, the ... splatting and the [] are not necessary. Furthermore, to improve the performance of your struct, you could specify type information of its fields:

struct MyEfficientStruct{T}
    a::T
    b::T
end

struct_vec = MyEfficientStruct.(2000:3000, 0)
struct_vec2 = MyEfficientStruct.(2000:3000, 0:1000)

Finally, while the broadcasting syntax is more terse than a loop, a big advantage of Julia is that the loop is just as fast (and sometimes faster) than the vectorized operation.

6
  • Thanks, what if I want the values for a go from 200 to 100 using 5 as step. (i.e. 200, 95, 90, 85, 80....)
    – M.E.
    Sep 16, 2019 at 9:43
  • I answer myself in case it helps others: vec = [MyStruct.(200:-5:100, 0)]
    – M.E.
    Sep 16, 2019 at 9:58
  • 2
    Here you are creating an array, then splatting it out with ..., and then collecting it back into an array again. That's highly redundant. Just do MyStruct.(2000:3000, 0).
    – DNF
    Sep 16, 2019 at 10:05
  • You're absolutely right! I'll add an edit later today to point this out.
    – Wolf
    Sep 16, 2019 at 10:14
  • 1
    @Wolf What I meant was, drop the brackets altogether. No splatting and no brackets. Just MyStruct.(2000:3000, 0).
    – DNF
    Sep 16, 2019 at 15:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.