Share millisecond extension

This commit is contained in:
Takashi Kokubun 2015-11-27 01:51:04 +09:00
parent 33b8484bfb
commit 574e724027
3 changed files with 45 additions and 44 deletions

View File

@ -4,6 +4,7 @@ require 'bundler/setup'
require 'hamlit'
require 'faml'
require 'benchmark/ips'
require_relative '../utils/benchmark_ips_extension'
Benchmark.ips do |x|
x.report("Faml::AB.build") { Faml::AttributeBuilder.build("'", true, nil, {:id=>"book"}, id: %w[content active]) }

View File

@ -0,0 +1,43 @@
# Monkey patch to show milliseconds
module Benchmark
module IPS
class Report
module EntryExtension
def body
return super if Benchmark::IPS.options[:format] != :human
left = "%s i/s (%1.3fms)" % [Helpers.scale(ips), (1000.0 / ips)]
iters = Helpers.scale(@iterations)
if @show_total_time
left.ljust(20) + (" - %s in %10.6fs" % [iters, runtime])
else
left.ljust(20) + (" - %s" % iters)
end
end
end
Entry.send(:prepend, EntryExtension)
end
end
module CompareExtension
def compare(*reports)
return if reports.size < 2
sorted = reports.sort_by(&:ips).reverse
best = sorted.shift
$stdout.puts "\nComparison:"
$stdout.printf "%20s: %10.1f i/s (%1.3fms)\n", best.label, best.ips, (1000.0 / best.ips)
sorted.each do |report|
name = report.label.to_s
x = (best.ips.to_f / report.ips.to_f)
$stdout.printf "%20s: %10.1f i/s (%1.3fms) - %.2fx slower\n", name, report.ips, (1000.0 / report.ips), x
end
$stdout.puts
end
end
extend CompareExtension
end

View File

@ -6,6 +6,7 @@ require 'faml'
require 'thor'
require 'benchmark/ips'
require 'hamlit1' if File.exist?('hamlit1')
require_relative '../benchmark/utils/benchmark_ips_extension'
class Bench < Thor
class_option :show_template, type: :boolean, aliases: ['-t']
@ -81,48 +82,4 @@ class Bench < Thor
end
end
# Monkey patch to show milliseconds
module Benchmark
module IPS
class Report
module EntryExtension
def body
return super if Benchmark::IPS.options[:format] != :human
left = "%s i/s (%1.3fms)" % [Helpers.scale(ips), (1000.0 / ips)]
iters = Helpers.scale(@iterations)
if @show_total_time
left.ljust(20) + (" - %s in %10.6fs" % [iters, runtime])
else
left.ljust(20) + (" - %s" % iters)
end
end
end
Entry.send(:prepend, EntryExtension)
end
end
module CompareExtension
def compare(*reports)
return if reports.size < 2
sorted = reports.sort_by(&:ips).reverse
best = sorted.shift
$stdout.puts "\nComparison:"
$stdout.printf "%20s: %10.1f i/s (%1.3fms)\n", best.label, best.ips, (1000.0 / best.ips)
sorted.each do |report|
name = report.label.to_s
x = (best.ips.to_f / report.ips.to_f)
$stdout.printf "%20s: %10.1f i/s (%1.3fms) - %.2fx slower\n", name, report.ips, (1000.0 / report.ips), x
end
$stdout.puts
end
end
extend CompareExtension
end
Bench.start