Skip to content

Commit f1c8109

Browse files
authored
[ruby/rails] Reduce random_id calls in update (#8856)
Calling `Array#sample` with a size x is faster than calling `rand` x times.
1 parent 770ef72 commit f1c8109

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def fortune
3333
end
3434

3535
def update
36-
worlds = Array.new(query_count) do
37-
world = World.find(random_id)
36+
worlds = ALL_IDS.sample(query_count).map do |id|
37+
world = World.find(id)
3838
new_value = random_id
3939
new_value = random_id until new_value != world.randomNumber
4040
world.update_columns(randomNumber: new_value)

0 commit comments

Comments
 (0)