Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ getivar:
desc: getivar tests the performance of getting instance variable values.
category: micro
single_file: true
attr_accessor:
desc: attr_accessor tests the performance of getting instance variable values via an attr_accessor imemo.
category: micro
single_file: true
keyword_args:
desc: keyword_args tests the performance of method calls with keyword arguments.
category: micro
Expand Down
41 changes: 41 additions & 0 deletions benchmarks/attr_accessor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative '../harness/loader'

class TheClass
attr_accessor :levar

def initialize
@v0 = 1
@v1 = 2
@v3 = 3
Comment on lines +7 to +9
Copy link
Member

Choose a reason for hiding this comment

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

This makes me uncomfortable lol

I think you meant either

Suggested change
@v0 = 1
@v1 = 2
@v3 = 3
@v0 = 1
@v1 = 2
@v2 = 3

or

Suggested change
@v0 = 1
@v1 = 2
@v3 = 3
@v1 = 1
@v2 = 2
@v3 = 3

Copy link
Member

Choose a reason for hiding this comment

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

and I guess it was just copied from the getivar benchmark. let's just fix both

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I just deleted the @. Oop

@levar = 1
end

def get_value_loop
sum = 0

# 1M
i = 0
while i < 1000000
# 10 times to de-emphasize loop overhead
sum += levar
sum += levar
sum += levar
sum += levar
sum += levar
sum += levar
sum += levar
sum += levar
sum += levar
sum += levar
i += 1
end

return sum
end
end

obj = TheClass.new

run_benchmark(850) do
obj.get_value_loop
end