InfraRuby Vision delivers solutions for mobile and web applications.
You can try InfraRuby live in your browser!
Try the InfraRuby statically typed Ruby compiler live in your browser. Try our examples or write your own code.
Follow @InfraRuby on Twitter for news and updates.

Benchmark ✦ Examples

InfraRuby is a compiler and runtime for statically typed Ruby. You can use InfraRuby for your own projects with our free download!

coding style

The InfraRuby compiler supports many coding styles. In particular, you may use either spaces or tabs for indentation and you may omit the return keyword where applicable.

For more information:

This example implements the benchmark standard library feature.

Benchmark

## <>
module Benchmark
	class << self
		## &{Report -> void} ->  void
		## Integer, &{Report -> void} ->  void
		def bm(width = nix)
			width = 0 if width.nix?
			STDOUT.write(" " * width)
			STDOUT.write("       user     system      total        real\n")
			yield Report.new(width)
			return
		end

		## &{-> void} -> Tms
		## String, &{-> void} -> Tms
		def measure(label = nix)
			label = "" if label.nix?
			s0 = Process.times
			t0 = Time.now
			yield
			s1 = Process.times
			t1 = Time.now
			return __new_tms(s0, s1, t1 - t0, label)
		end

		## Process::Tms, Process::Tms, Float, String -> Tms
		def __new_tms(s0, s1, real, label)
			return Tms.new(
				s1.utime - s0.utime,
				s1.stime - s0.stime,
				s1.cutime - s0.cutime,
				s1.cstime - s0.cstime,
				real,
				label
			)
		end
	end
end

Benchmark::Report

module Benchmark
	## <>
	##   @width: Integer
	class Report
		## Integer -> void
		def initialize(width)
			@width = width
			return
		end

		## &{-> void} -> void
		## String, &{-> void} -> void
		def report(label = nix, &block)
			label = "" if label.nix?
			tms = Benchmark.measure(label.ljust(@width), &block)
			STDOUT.write(tms.to_s)
			return
		end
	end
end

Benchmark::Tms

module Benchmark
	## <>
	##   @utime: Float
	##   @stime: Float
	##   @cutime: Float
	##   @cstime: Float
	##   @real: Float
	##   @label: String
	class Tms
		## Float, Float, Float, Float, Float, String -> void
		def initialize(utime, stime, cutime, cstime, real, label)
			@utime = utime
			@stime = stime
			@cutime = cutime
			@cstime = cstime
			@real = real
			@label = label
			return
		end

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

		## -> String
		def to_s
			return format
		end

		## Float -> Tms
		def *(f)
			return Tms.new(
				@utime * f,
				@stime * f,
				@cutime * f,
				@cstime * f,
				@real * f,
				@label
			)
		end

		## Float -> Tms
		def /(f)
			return Tms.new(
				@utime / f,
				@stime / f,
				@cutime / f,
				@cstime / f,
				@real / f,
				@label
			)
		end

		## Tms -> Tms
		def +(o)
			return Tms.new(
				@utime + o.utime,
				@stime + o.stime,
				@cutime + o.cutime,
				@cstime + o.cstime,
				@real + o.real,
				@label
			)
		end

		## Tms -> Tms
		def -(o)
			return Tms.new(
				@utime - o.utime,
				@stime - o.stime,
				@cutime - o.cutime,
				@cstime - o.cstime,
				@real - o.real,
				@label
			)
		end

		## -> String
		def format
			return "%s %10.6f %10.6f %10.6f (%10.6f)\n" % [
				@label,
				@utime,
				@stime,
				@utime + @stime + @cutime + @cstime,
				@real
			]
		end
	end
end
Follow @InfraRuby on Twitter
Copyright © 2011-2017 InfraRuby Vision Limited