Skip to content
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

Speed up sin and cos by a factor 6.5 #117

Merged
merged 4 commits into from
Apr 11, 2018
Merged

Speed up sin and cos by a factor 6.5 #117

merged 4 commits into from
Apr 11, 2018

Conversation

dpsanders
Copy link
Member

@dpsanders dpsanders commented Apr 9, 2018

Current master:

julia> x = 3..4
[3, 4]

julia> @btime sin($x)
  648.567 ns (14 allocations: 448 bytes)
[-0.756803, 0.141121]

This PR:

julia> @btime sin($x)
  94.037 ns (0 allocations: 0 bytes)
[-0.756803, 0.141121]

@dpsanders dpsanders changed the title Add docs on interval construction Speed up trig functions by 6 times Apr 9, 2018
@dpsanders dpsanders force-pushed the faster_trig branch 2 times, most recently from 72ea4dd to b7b1d03 Compare April 10, 2018 16:54
@dpsanders
Copy link
Member Author

I have not found a sensible way to do tan correctly.
We will be able to use #119 to calculate the quadrant on Julia 0.7.

Hence leaving tan aside for now.

@dpsanders dpsanders changed the title Speed up trig functions by 6 times Speed up sin and cos by a factor 6.5 Apr 11, 2018
@lbenet
Copy link
Member

lbenet commented Apr 11, 2018

Is this ready for review? Ping me when it is ready. (Regarding tan, we can leave it for another PR...)

@dpsanders dpsanders mentioned this pull request Apr 11, 2018
@dpsanders
Copy link
Member Author

dpsanders commented Apr 11, 2018

Yes, ready for review.
tan will have to wait for Julia 0.7 / 1.0 (due any time now...)

@lbenet

@dpsanders dpsanders requested a review from lbenet April 11, 2018 21:46
@codecov-io
Copy link

codecov-io commented Apr 11, 2018

Codecov Report

Merging #117 into master will decrease coverage by 0.15%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #117      +/-   ##
==========================================
- Coverage   92.81%   92.65%   -0.16%     
==========================================
  Files          21       21              
  Lines         960      967       +7     
==========================================
+ Hits          891      896       +5     
- Misses         69       71       +2
Impacted Files Coverage Δ
src/intervals/trigonometric.jl 99.15% <100%> (-0.85%) ⬇️
src/intervals/precision.jl 87.5% <0%> (-4.17%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 373b19b...776e419. Read the comment docs.

@coveralls
Copy link

coveralls commented Apr 11, 2018

Coverage Status

Coverage decreased (-0.2%) to 92.658% when pulling 776e419 on faster_trig into 373b19b on master.

Copy link
Member

@lbenet lbenet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have submitted two optional suggestions; I think this can be merged.

Multiply an interval by a positive constant.
For efficiency, does not check that the constant is positive.
"""
multiply_by_positive_constant(α, x::Interval) = @round(α*x.lo, α*x.hi)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps instead of defining multiply_by_positive_constant we should rather define the methods *(a::T, x::Interval) and *(x::Interval, a::T) (in arithmetic.jl), where T<:Real (or maybe <:Number), or even unspecified, as it is here. Currently we use promotion to an interval and rounding, which may slow down things.

Question: Below you use this function for to define half_pi(::Type{Float64}), but not for other types; can you explain why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use it for two_pi as well.

Actually the half_pi function is no longer used, I believe.

We may well want to define explicit methods for *(constant, Interval). I've opened an issue: #120

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was actually the suggestion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but I'd prefer to leave it for a separate PR.
Also, if you actually know that the constant is positive then this specialised version is even slightly quicker.

temp = atomic(Interval{T}, x) / half_pi(x)
(floor(temp.lo), floor(temp.hi))

return SVector(floor(temp.lo), floor(temp.hi))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about returning a tuple here, instead of a SVector? (Same comment applies in line 44.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to be a tuple; changing it to an SVector had a ridiculously big impact on the speed due to the minimum and maximum below that are not specialised for tuples!

In any case all of this code can be simplified a lot when #119 is possible, I believe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I found an alternative solution: return a tuple and do

max(tuple...)

Too late...

@dpsanders dpsanders merged commit 9d40ff3 into master Apr 11, 2018
@dpsanders dpsanders deleted the faster_trig branch April 11, 2018 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants