Add test for split_and_rotate

This commit is contained in:
Daniel Schädler 2025-03-02 16:35:19 +01:00
parent fdfea9100d
commit e07d40e2d4
1 changed files with 25 additions and 0 deletions

25
overengineering.rb Normal file
View File

@ -0,0 +1,25 @@
require 'benchmark'
def split_and_rotate(arr)
midpoint = arr.length / 2
arr[0...midpoint] + arr[midpoint..-1]
end
# Create a large array with 1 million entries
large_array = (1..1_000_000).to_a
# Run the operation 1000 times and calculate the average time
total_time = 0
iterations = 1000
iterations.times do
time_taken = Benchmark.realtime do
split_and_rotate(large_array)
end
total_time += time_taken
# shuffle the array to avoid caching
large_array.shuffle!
end
average_time = total_time / iterations
puts "Average time taken over #{iterations} iterations: #{average_time} seconds"