#!/usr/bin/env ruby #-*-ruby-*- require "catbuf" TRYCOUNT = 500000 def bench(comment, &block) GC.start start_times = Time.times TRYCOUNT.times(&block) finish_times = Time.times puts "#{comment}\n user: #{finish_times.utime - start_times.utime}, system: #{finish_times.stime - start_times.stime}" end str = "" appended = "12345678" bench("Appending to String") { str << appended } str = "\000" * (TRYCOUNT * appended.size) str.slice!(0, 0) bench("Appending to String with buffer allocated") { str << appended } str = CatBuffer.new bench("Appending to CatBuffer") { str << appended } str = CatBuffer.new(TRYCOUNT * appended.size) bench("Appending to CatBuffer with enough capacity") { str << appended }