Skip to content

Performance problems when using broadcasting and logical operators #47493

@roflmaostc

Description

@roflmaostc
Contributor

Broadcasting with boolean operators seems to be very slow in some circumstances:

See below one example with Julia 1.8.2:

julia> x = randn((100,));

julia> y = randn((1,100));

julia> @time (x.^2 .< 1) .&& (1 .+ y.^2  .<1);
  0.004369 seconds (144.73 k allocations: 3.005 MiB)

julia> @btime ($x.^2 .< 1) .&& (1 .+ $y.^2  .<1);
  4.009 ms (144713 allocations: 3.00 MiB)

# this works
julia> @btime ($x.^2 .< 1) .&& ($y.^2  .<1);
  2.523 μs (3 allocations: 5.56 KiB)

julia> @btime ($x.^2 .< 1) .|| ($y.^2 .+ 1 .<1);
  2.738 μs (3 allocations: 5.56 KiB)

Activity

changed the title [-]Performance regression allocations when using broadcasting and logical operators[/-] [+]Performance regression when using broadcasting and logical operators[/+] on Nov 8, 2022
changed the title [-]Performance regression when using broadcasting and logical operators[/-] [+]Performance problems when using broadcasting and logical operators[/+] on Nov 8, 2022
N5N3

N5N3 commented on Nov 8, 2022

@N5N3
Member

Our .&& and .|| is flatten based so I believe this is a duplication of #27988.
With #43322, I get

julia> @btime ($x.^2 .< 1) .&& (1 .+ $y.^2  .<1);
  10.400 μs (3 allocations: 5.56 KiB)

julia> @btime ($x.^2 .< 1) .& (1 .+ $y.^2  .<1);
  4.271 μs (3 allocations: 5.56 KiB)

julia> @btime ($x.^2 .< 1) .&& ($y.^2  .<1);
  10.000 μs (3 allocations: 5.56 KiB)

julia> @btime ($x.^2 .< 1) .& ($y.^2  .<1);
  4.229 μs (3 allocations: 5.56 KiB)

So perhaps .& is always a better choice here.

added
broadcastApplying a function over a collection
duplicateIndicates similar issues or pull requests
on Nov 8, 2022
added a commit that references this issue on Jul 15, 2023
f15eb4e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    broadcastApplying a function over a collectionduplicateIndicates similar issues or pull requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @roflmaostc@N5N3

      Issue actions

        Performance problems when using broadcasting and logical operators · Issue #47493 · JuliaLang/julia