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

Add faster while loop implementations and new Time#+ benchmark #155

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bawNg
Copy link

@bawNg bawNg commented May 2, 2018

No description provided.

bawNg added 2 commits May 2, 2018 16:24
Reduced benchmark overhead
Ruby 2.5 reduces each_with_index overhead

def faster
index = 0
size = ARRAY.size
Copy link
Collaborator

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 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

Copy link
Collaborator

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* vs while not different memoization tricks:

require "benchmark/ips"

ARRAY = [*1..100]

def fastest
  index = 0
  size  = ARRAY.size

  while index < size
    ARRAY[index] + index
    index += 1
  end

  ARRAY
end

def faster
  index = 0

  ARRAY.each do |number|
    number + index
    index += 1
  end

  ARRAY
end

def slow
  ARRAY.each_with_index { |number, index| number + index }
end

Benchmark.ips do |x|
  x.report("fastest", 'fastest;' * 1000)
  x.report("faster", 'faster;' * 1000)
  x.report("slow", 'slow;' * 1000)
  x.compare!
end

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  slower

Copy link
Author

@bawNg bawNg May 2, 2018

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.

Copy link
Collaborator

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.

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.

2 participants