Skip to content

Instantly share code, notes, and snippets.

@knugie
Last active December 19, 2015 02:48
Show Gist options
  • Save knugie/5885225 to your computer and use it in GitHub Desktop.
Save knugie/5885225 to your computer and use it in GitHub Desktop.
Quick "who's better" - Benchmark script
require 'benchmark'
class Bunch
def self.mark(&block)
first = Bunch.realtime(&block)
vs = {:first => first}
class << vs
def vs(&block)
self[:second] = Bunch.realtime(&block)
self[:winner] = self.invert.sort[0][1]
self
end
end
return vs
end
def self.realtime(&block)
tmp_stderr, $stderr = $stderr.dup, StringIO.new
tmp_stdout, $stdout = $stdout.dup, StringIO.new
Benchmark.realtime(&block)
ensure
$stderr, $stdout = tmp_stderr, tmp_stdout
end
end
Bunch.mark{puts ((('.'*10)*10)*10)*10}.vs{puts '.'*10000}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment