Ring Benchmark

July 24, 2019

This script creates a ring of arbitrary number of nodes. Every node except the last one have a link to the next created node. The number of nodes is specified through the command line argument. There's a number which is transfered from one node to another. Every time that number is increased by 1. When the program reaches the last node it transfers the control back to the first node (if a second command line argument is greater than 1). The program continues to transfer the number round the ring and increase its value. After it finishes to run the number through the ring it prints out the time of that work.

View Demo Download

  • Level: Very Easy
  • Duration: 10 minutes
  • Author: Master

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]

1 comment

  1. tsiger - January 21, 2011 1:43 pm

    Proin quis tortor orci. Etiam at risus et justo dignissim congue. Donec congue lacinia dui, a porttitor lectus condimentum laoreet. Nunc eu ullamcorper orci. Quisque eget odio ac lectus vestibulum faucibus eget in metus. In pellentesque faucibus vestibulum. Nulla at nulla justo, eget luctus tortor. Nulla facilisi. Duis aliquet egestas purus in blandit. Curabitur vulputate, ligula lacinia scelerisque tempor, lacus lacus ornare ante, ac egestas est urna sit amet arcu. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed molestie augue sit amet leo consequat posuere. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin vel ante a orci tempus eleifend ut et magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus luctus urna sed urna ultricies ac tempor dui sagittis. In condimentum facilisis porta. Sed nec diam eu diam mattis viverra. Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin sapien justo in libero. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum. Donec viverra auctor lobortis. Pellentesque eu est a nulla placerat dignissim. Morbi a enim in magna semper bibendum. Etiam scelerisque, nunc ac egestas consequat, odio nibh.

    Reply

Have your say