Hang during conversion


I have a 2D array of 0s and 1s stored as Int64. I want to convert them to booleans and tried the following statement but my computer hangs. Would you please describe why?

a = [1 0 1; 0 1 1];
b=[Bool(x) for x in a…]


Try Bool.(a). The reason it hangs is likely the splatting .... It is not a good idea to splat a large number of arguments.


You could also try BitArray(a). Is a BitArray better than an Array{Bool,N} ?


It depends a bit of what you will use the array for later. It will take less space but is slower in some usecases.


You can use a comprehension, but don’t use splatting here: Use [Bool(x) for x in a]. It’s not just inefficient to splat, it makes no sense. I’m surprised it works at all.

But it’s clearly even better to use Bool.(a) or BitArray.


I don’t think it works at all, it just takes a long time to error.


Actually Bool.(a) returns a BitArray, so the result is the same, but BitArray(a) is a bit more efficient (mainly in terms of memory). A much faster way however is a .== 1.


julia> A = rand(0:1, 100, 100);

julia> @btime Bool.($A);
  43.683 μs (3 allocations: 5.56 KiB)

julia> @btime BitArray($A);
  40.027 μs (2 allocations: 1.38 KiB)

julia> @btime $A .== 1;
  9.132 μs (3 allocations: 5.56 KiB)