# @fastmath for library functions? (2x slowdown of binary search because of NaN checks in Base)

**URL:** <https://discourse.julialang.org/t/fastmath-for-library-functions-2x-slowdown-of-binary-search-because-of-nan-checks-in-base/93487>\
**Category:** Performance\
**Tags:** fast-math\
**Created:** [January 25, 2023, 3:19am UTC](https://discourse.julialang.org/t/fastmath-for-library-functions-2x-slowdown-of-binary-search-because-of-nan-checks-in-base/93487 "2023-01-25T03:19:56Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [January 25, 2023, 4:50pm UTC](https://discourse.julialang.org/t/fastmath-for-library-functions-2x-slowdown-of-binary-search-because-of-nan-checks-in-base/93487/6 "2023-01-25T16:50:45Z")

</div>

> [@Sukera](#):
>
> Yes there is, surprisingly enough (though it doesn’t make a difference in the end for this):

This function does make a big difference in some cases.

```julia
julia> x = rand(512);

julia> @btime minimum($x)
  848.195 ns (0 allocations: 0 bytes)
0.0004658392690449764

julia> @btime reduce((x,y) -> x<y ? x : y, $x, init=Inf)
  395.203 ns (0 allocations: 0 bytes)
0.0004658392690449764

julia> @btime reduce((x,y) -> @fastmath(x < y) ? x : y, $x, init=Inf)
  33.046 ns (0 allocations: 0 bytes)
0.0004658392690449764

```

`@fastmath` isn’t just about swapping out one implementation for another, but about communicating assumptions to the compiler about transformations it is allowed to apply.  
In this case, it tells the compiler it is allowed to do a SIMD reduction.

Doing so risks missing NaN values, but if we promise no-nans, then reordering this reduction sequence is suddenly legal.

---

_[View the full topic](https://discourse.julialang.org/t/fastmath-for-library-functions-2x-slowdown-of-binary-search-because-of-nan-checks-in-base/93487)._
