-
Notifications
You must be signed in to change notification settings - Fork 377
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
Add faster while loop implementations and new Time#+ benchmark #155
Open
bawNg
wants to merge
2
commits into
fastruby:main
Choose a base branch
from
bawNg:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'benchmark/ips' | ||
|
||
TimeFloatLowPrecision = 1525262447.0 | ||
OffsetFloatLowPrecision = 1.1 | ||
TimeFloatHighPrecision = 1525262447.1234567 | ||
OffsetFloatHighPrecision = 1.1234567 | ||
|
||
TimeLowPrecision = Time.at(TimeFloatLowPrecision) | ||
TimeHighPrecision = Time.at(TimeFloatHighPrecision) | ||
|
||
def fast_low_precision | ||
Time.at(TimeLowPrecision.to_f + OffsetFloatLowPrecision) | ||
end | ||
|
||
def slow_low_precision | ||
TimeLowPrecision + OffsetFloatLowPrecision | ||
end | ||
|
||
def fast_high_precision | ||
Time.at(TimeHighPrecision.to_f + OffsetFloatHighPrecision) | ||
end | ||
|
||
def slow_high_precision | ||
TimeHighPrecision + OffsetFloatHighPrecision | ||
end | ||
|
||
Benchmark.ips do |x| | ||
x.report('Time#to_f+ (low)', 'fast_low_precision;' * 1000) | ||
x.report('Time#+ (low)', 'slow_low_precision;' * 1000) | ||
x.report('Time#to_f+ (high)', 'fast_high_precision;' * 1000) | ||
x.report('Time#+ (high)', 'slow_high_precision;' * 1000) | ||
x.compare! | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
fastest
and currentfastest
should be removed, as they will always will be around the same. Constant lookup is not slower than local variable in terms of code execution. It's not even micro-optimization it some sort of pseudo-scientific nano-optimization :DThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact whole benchmark seems a bit wrong to me. It compares oranges to potatoes. It should test
each*
vswhile
not different memoization tricks:Results will be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about removing the fastest micro optimized version but I decided to include it because local variables should be able to be optimized much better than other lookups. The difference may be very small with the current Ruby implementation but benchmarks do consistently show a performance difference in many cases even if it's overkill for most use cases. The Linux benchmarks I ran were up to 12 i/s faster which is 12k calls per second and an average of 19 i/s faster under Windows (constants are 1.04x slower). I'm running these benchmarks with high CPU priority on Windows and real-time scheduling on Linux to maximize consistency.
As for whether the benchmark should be comparing different memoization tricks or not, that may be a good reason to only include one while loop but when comparing a while loop to a native abstraction, it should ideally be written as optimally as possible. Array#each_with_index gets to take advantage of the C stack and avoid all the overhead of any kind of ruby variable when it comes to referencing the array and doing comparisons and increments on the index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that local variable will be slower on such synthetic tests. But in real world example where payload of iteration is something less stupid it will seek to nothing. And I think the aim of this project is providing somewhat "better practices". If real code suffers from overhead of local var vs constant lookup - i would consider it as ruby issue.