html5 – Euclides http://ezisetup.com/designs/euclides Just another EziSite Mon, 24 Feb 2014 22:11:53 +0000 en-US hourly 1 Ring Benchmark http://ezisetup.com/designs/euclides/ring-benchmark/ http://ezisetup.com/designs/euclides/ring-benchmark/#comments Fri, 21 Jan 2011 12:38:40 +0000 http://www.cssigniter.com/preview/euclides/?p=19 This script is based on fibers — a Ruby realisation of so-called coroutines. It can serve as an example which demonstrates how fibers works.

The code

[ruby]
# encoding: utf-8

require ‘benchmark’

class Node
@@number = 0

def initialize(attach = nil)
@attach = attach
@fiber = Fiber.new { pass_number }
end

def pass_number
loop do
@attach ? @@number = @attach.resume.succ
: @@number = @@number.succ
Fiber.yield @@number
end
end

def resume
@fiber.resume
end

def reset_number
@@number = 0
end
end

class Gauge
def initialize
@nodes = $*[0].to_i
@cycles = $*[1].to_i
create_ring
end

def create_ring
if @nodes <= 1
raise ArgumentError, ‘the node quantity should be greater than 1’
else
@nodes.times do
@ring ? @ring = Node.new(@ring)
: @ring = Node.new
end
end
end

def run
time = Benchmark.measure do
@cycles.times { @ring.resume }
end.format(‘%.3r’).gsub!(/(|)/, ”)
puts time
@ring.reset_number
end

def self.start
new.run
end
end

Gauge.start
[/ruby]

]]>
http://ezisetup.com/designs/euclides/ring-benchmark/feed/ 1