-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple broadcast expressions expansion #22376
Comments
In short: Don't benchmark in global scope.
julia> @time f.(g.(x)); # warm up
0.876215 seconds (64.20 k allocations: 3.565 MiB)
julia> @time f.(g.(x));
0.054706 seconds (4.38 k allocations: 244.556 KiB)
julia> foo(x) = f.(g.(x));
julia> @time foo(x); # warm up
0.050510 seconds (14.72 k allocations: 681.195 KiB)
julia> @time foo(x);
0.000005 seconds (5 allocations: 272 bytes) This should then be faster than |
Thanks - I wasn't aware of that. 0.6.0-rc3.0> using DataArrays
0.6.0-rc3.0> x = DataArray(1:3);
0.6.0-rc3.0> foo(x) = .!isna.(x)
foo (generic function with 1 method)
0.6.0-rc3.0> foo(x)
3-element DataArrays.DataArray{Bool,1}:
true
true
true
0.6.0-rc3.0> bar(x) = (y = isna.(x); .!y)
bar (generic function with 1 method)
0.6.0-rc3.0> bar(x)
3-element BitArray{1}:
true
true
true i.e. the return of |
Or, a slightly contrived example with types from Base only, which has different return values: 0.6.0-rc3.0> f(x) = x != 2
f (generic function with 1 method)
0.6.0-rc3.0> x = SparseVector(10, [1,2,5,9,10], [1,2,3,4,5])
10-element SparseVector{Int64,Int64} with 5 stored entries:
[1 ] = 1
[2 ] = 2
[5 ] = 3
[9 ] = 4
[10] = 5
0.6.0-rc3.0> .!f.(x)
10-element SparseVector{Bool,Int64} with 1 stored entry:
[2 ] = true
0.6.0-rc3.0> .!begin f.(x) end
10-element SparseVector{Bool,Int64} with 10 stored entries:
[1 ] = false
[2 ] = true
[3 ] = false
[4 ] = false
[5 ] = false
[6 ] = false
[7 ] = false
[8 ] = false
[9 ] = false
[10] = false |
Please reopen, @martinholters :) |
DataArrays specializes julia> x = DataArray(1:3);
julia> typeof(isna.(x))
BitArray{1}
julia> _isna(x) = isna(x)
_isna (generic function with 1 method)
julia> typeof(_isna.(x))
DataArrays.DataArray{Bool,1} I'd say that's an issue of DataArrays, not Base. And the two results for |
Ahh - so they are. Interesting that the representation differs, though. |
The code expansion for broadcast expressions such as
f.(g.(x))
looks to be incorrect (involving:composite_type
, for example). For some vector types, this is also leading to incorrect return types (see JuliaStats/DataArrays.jl#263).versus the more intuitive expansion when the inner expression is put inside a
begin...end
block:It is also very slow, even for very simple expressions:
(each ran a few times to burn-in)
compared to:
The text was updated successfully, but these errors were encountered: