rmagick bench mark test
#-*- coding: utf-8 -*-
require 'benchmark'
# When you convert a jpg image using ImageMagick,
# convert command options combination make a big difference in performance.
# so i try to do a benchmark test about parameter combination.
#
# Fastest combination : when given define option with width and height.
# Secondary combination : when given define with either width or height, debug option make faster.
# Thirdly combination : when given define with either width or height.
# Slowest : no given define and debug.
def resize(options)
# ImageMagick's option parameter combination
size = options[:size] || '180x120'
command = ['convert']
command << '-debug Coder -log %e' if options[:debug]
command << '-define jpeg:size='+size if options[:define]
command << '-resize'
command << size
command << options[:filename]
command << 'after_' + options[:filename].downcase
system(*command)
end
# 50 image file were prepared.
list = Dir::glob('./*.JPG').map {|item|
File.basename(item)
}
# test it.
Benchmark.bm(30) {|x|
x.report("no debug & define:") {
list.each {|item| resize(:filename => item )}
}
x.report("with debug:") {
list.each {|item| resize(:filename => item, :debug => true, :define => true)}
}
x.report("with define:") {
list.each {|item| resize(:filename => item, :define => true)}
}
x.report("no define size=x600:") {
list.each {|item| resize(:filename => item, :size=> "x600")}
}
x.report("with define size=x600:") {
list.each {|item| resize(:filename => item, :size=> "x600", :define => true)}
}
x.report("with define size=x120:") {
list.each {|item| resize(:filename => item, :size => "x120", :define => true)}
}
x.report("with define size=800x600:") {
list.each {|item| resize(:filename => item, :size => "800x600", :define => true)}
}
x.report("with define & debug size=x120:") {
list.each {|item| resize(:filename => item, :size => "x120", :define => true, :debug => true)}
}
x.report("with define & debug size=x600:") {
list.each {|item| resize(:filename => item, :size => "x600", :define => true, :debug => true)}
}
}
=begin
Result:
-------------------------------------------------------------------------------
user system total real
no debug & define : 0.070000 0.780000 243.620000 (137.978237)
with debug & define : 0.000000 0.260000 30.860000 ( 24.966679)
no define size=x600 : 0.020000 0.940000 289.010000 (156.380973)
with define size=x600 : 0.040000 0.890000 282.290000 (153.480008)
with define size=x120 : 0.060000 0.940000 252.440000 (144.542583)
with define size=800x600 : 0.040000 0.480000 96.330000 ( 59.767320)
with define & debug size=x120 : 0.030000 0.260000 36.170000 ( 27.981825)
with define & debug size=x600 : 0.080000 0.410000 106.170000 ( 62.490479)
=end