| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| 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 |
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis should be fastest and current fastest 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 :D
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIn fact whole benchmark seems a bit wrong to me. It compares oranges to potatoes. It should test each* vs while not different memoization tricks:
Results will be:
Warming up -------------------------------------- fastest 37.000 i/100ms faster 23.000 i/100ms slow 24.000 i/100ms Calculating ------------------------------------- fastest 375.210 (± 1.1%) i/s - 1.887k in 5.029639s faster 242.769 (± 2.1%) i/s - 1.219k in 5.023745s slow 236.484 (± 1.3%) i/s - 1.200k in 5.074932s Comparison: fastest: 375.2 i/s faster: 242.8 i/s - 1.55x slower slow: 236.5 i/s - 1.59x slowerSorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.