[#20083] non-block IO with TCPSocket — dn <daisuke@...>

初投稿の中村と申します。よろしくお願いします。

19 messages 2000/01/06
[#20084] Re: non-block IO with TCPSocket — Tomoyuki Kosimizu <greentea@...2.so-net.ne.jp> 2000/01/06

越水です。

[#20091] Re: non-block IO with TCPSocket — とみたまさひろ <tommy@...> 2000/01/06

とみたです。

[#20133] おききしたーいでーす — akimaru <akimaru@...>

17 messages 2000/01/09
[#20138] Re: おききしたーいでーす — akimaru <akimaru@...> 2000/01/09

[#20237] Ruby/Tk multi interpreter — nagai@...

永井@知能.九工大です.

21 messages 2000/01/17
[#20242] Re: Ruby/Tk multi interpreter — nagai@... 2000/01/17

永井@知能.九工大です.

[#20248] Re: Ruby/Tk multi interpreter — Hideto ISHIBASHI <s34204@...> 2000/01/17

石橋秀仁です。

[#20254] Re: Ruby/Tk multi interpreter — nagai@... 2000/01/18

永井@知能.九工大です.

[#20271] Re: Ruby/Tk multi interpreter — Hideto ISHIBASHI <s34204@...> 2000/01/18

石橋秀仁です。

[#20249] FTP.open err for Windows95 — "Y Kataoka" <kataoka@...>

初めまして、片岡@KLUGと申します。

18 messages 2000/01/17
[#20252] Re: FTP.open err for Windows95 — "NAKAMURA, Hiroshi" <nakahiro@...> 2000/01/18

なひです.

[#20342] How to build ruby(current) with cygwin — KORIYAMA Naohiro <kory@...2.so-net.ne.jp>

はじめまして、こおりやまです。

19 messages 2000/01/23
[#20362] Re: How to build ruby(current) with cygwin — WATANABE Hirofumi <Hirofumi.Watanabe@...> 2000/01/24

わたなべです.

[#20422] Re: How to build ruby(current) with cygwin — Masaki Suketa<CQN02273@...> 2000/01/29

Win32OLE の作者の助田です.

[#20394] ruby-1.4.3 port to HPUX 11.00 — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

15 messages 2000/01/26

[ruby-list:19998] benchmark.rb

From: gotoken@... (GOTO Kentaro)
Date: 2000-01-01 01:18:23 UTC
List: ruby-list #19998
ごとけんです

In message "[ruby-list:19997] Re: about for-in"
    on 00/01/01, Tomoyuki Kosimizu <greentea@fa2.so-net.ne.jp> writes:

>t1 = Time.now
[...]
>t2 = Time.now
>
>puts(t2 - t1)

こおいう測定は僕もよくやるので、時間の計測を簡便に行うための
ブツを書いてみました。

% cat hoge.rb
  require "benchmark"
  include Benchmark

  n = ARGV[0].to_i.nonzero? || 50000
  benchmark("       " + CAPTION, 
	    "for:   " + FMTSTR, 
	    "times: " + FMTSTR,
	    "upto:  " + FMTSTR) do
    [
      measure{for i in 1..n; a = "1"; end},  # Benchmark::measure
      measure{n.times do   ; a = "1"; end},
      measure{1.upto(n) do ; a = "1"; end}
    ]
  end
% ruby hoge.rb
             user     system      total        real
for:     1.016667   0.000000   1.016667 (  0.504279)
times:   1.433333   0.000000   1.433333 (  0.680590)
upto:    1.466667   0.000000   1.466667 (  0.688725)
%

なお、benchmark の引数は省略可能です。改良は大歓迎 :-)

てなわけで、今年もよろしくなのです。

-- gotoken


Attachments (1)

benchmark.rb (3.14 KB, text/x-ruby)
# benchmark.rb
#
# chagelog
#   01 Jan 2000: first release (gotoken@notwork.org)

module Benchmark
  def realtime(&blk)
    Benchmark::measure(&blk).real
  end

  def benchmark(caption = "", *fmt, &blk)
    raise ArgumentError, "no block" unless iterator?
    report(caption, *fmt, &blk)
  end

  def measure
    t0, r0 = Time.times, Time.now
    yield
    t1, r1 = Time.times, Time.now
    Benchmark::Tms.new(t1.utime  - t0.utime, 
		       t1.stime  - t0.stime, 
		       t1.cutime - t0.cutime, 
		       t1.cstime - t0.cstime, 
		       r1.to_f - r0.to_f)
  end

  def report(*args)
    if iterator?
      print args.shift
      tms = yield
      tms.each{|t|
	print t.format(fmt = args.shift)
      }
    else
      print Benchmark::CAPTION
      args.each{|t|
	print t
      }
    end
  end

  module_function :measure, :realtime, :report

  class Tms
    CAPTION = "      user     system      total        real\n"
    FMTSTR = "%10.6u %10.6y %10.6t %10.6r\n"

    attr_reader :utime, :stime, :cutime, :cstime, :real, :total

    def initialize(u = 0.0, s = 0.0, cu = 0.0, cs = 0.0, real = 0.0)
      @utime, @stime, @cutime, @cstime, @real = u, s, cu, cs, real
      @total = @utime + @stime + @cutime + @cstime
    end

    def add(&blk)
      self + Benchmark::measure(&blk) 
    end

    def add!
      t = Benchmark::measure(&blk) 
      @utime  = utime + t.utime
      @stime  = stime + t.stime
      @cutime = cutime + t.cutime
      @cstime = cstime + t.cstime
      @real   = real + t.real
      self
    end

    def +(x); memberwise(:+, x) end
    def -(x); memberwise(:-, x) end
    def *(x); memberwise(:*, x) end
    def /(x); memberwise(:/, x) end

    def format(arg0 = nil,*args)
      fmtstr = arg0 ? arg0 : Benchmark::Tms::FMTSTR.dup
      fmtstr.gsub!(/(%[-+\.\d]*)u/){"#{$1}f" % utime}
      fmtstr.gsub!(/(%[-+\.\d]*)y/){"#{$1}f" % stime}
      fmtstr.gsub!(/(%[-+\.\d]*)U/){"#{$1}f" % cutime}
      fmtstr.gsub!(/(%[-+\.\d]*)Y/){"#{$1}f" % cstime}
      fmtstr.gsub!(/(%[-+\.\d]*)t/){"#{$1}f" % total}
      fmtstr.gsub!(/(%[-+\.\d]*)r/){"(#{$1}f)" % real}
      arg0 ? Kernel::format(fmtstr, *args) : fmtstr
    end

    def to_s
      format
    end

    def to_a
      [@utime, @stime, @cutime, @cstime, @real]
    end

    protected
    def memberwise(op, x)
      case x
      when Benchmark::Tms
	Benchmark::Tms.new(utime.__send__(op, x.utime),
			   stime.__send__(op, x.stime),
			   cutime.__send__(op, x.cutime),
			   cstime.__send__(op, x.cstime),
			   real.__send__(op, x.real)
			   )
      else
	Benchmark::Tms.new(utime.__send__(op, x),
			   stime.__send__(op, x),
			   cutime.__send__(op, x),
			   cstime.__send__(op, x),
			   real.__send__(op, x)
			   )
      end
    end
  end

  CAPTION = Benchmark::Tms::CAPTION
  FMTSTR = Benchmark::Tms::FMTSTR
end

if __FILE__ == $0
  include Benchmark

  n = ARGV[0].to_i.nonzero? || 50000
  puts %Q([#{n} times iterations of `a = "1"'])
  benchmark("       " + CAPTION, 
	    "for:   " + FMTSTR, 
	    "times: " + FMTSTR,
	    "upto:  " + FMTSTR) do
    [
      measure{for i in 1..n; a = "1"; end},  # Benchmark::measure
      measure{n.times do   ; a = "1"; end},
      measure{1.upto(n) do ; a = "1"; end}
    ]
  end
end

In This Thread

Prev Next