Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created July 2, 2011 15:54
Show Gist options
  • Save agmcleod/1060926 to your computer and use it in GitHub Desktop.
Save agmcleod/1060926 to your computer and use it in GitHub Desktop.
test_sort.rb
require 'benchmark'
class Student
attr_accessor :name, :age
def initialize(args)
args.each do |k, v|
send("#{k}=".to_sym, v)
end
end
end
students = []
1_000_000.times do |i|
students << Student.new(
name: 'a student',
age: rand(7) + 18
)
end
Benchmark.bm(10) do |b|
b.report('sort') do
students.sort! { |a, b| a.age <=> b.age }
end
end
File.open('students_out.txt', 'w+') do |f|
students.each do |std|
f.write "#{std.age}\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment