Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fast-track
@threads
when nthreads() == 1
This avoids overhead when threading is disabled, Example benchmark: ``` using BenchmarkTools, Base.Threads function func(val) sum = 0*(1 .^ val) for idx in 1:100 sum += idx.^val end return sum end function func_threaded(val) sum = 0*(1 .^ val) @threads for idx in 1:100 sum += idx.^val end return sum end function func_threaded_ref(val) sum = Ref(0*(1 .^ val)) @threads for idx in 1:100 sum[] += idx.^val end return sum[] end @show @benchmark func(2.0) @show @benchmark func_threaded(2.0) @show @benchmark func_threaded_ref(2.0) ``` Before change: ``` @benchmark(func(2.0)) = Trial(94.020 ns) @benchmark(func_threaded(2.0)) = Trial(5.623 μs) @benchmark(func_threaded_ref(2.0)) = Trial(3.913 μs) ``` After change: ``` @benchmark(func(2.0)) = Trial(93.124 ns) @benchmark(func_threaded(2.0)) = Trial(2.175 μs) @benchmark(func_threaded_ref(2.0)) = Trial(554.128 ns) ```
- Loading branch information