Sha256: 462f9637addd01e77e36c67c3f531ab8046eb944f39381470a2e565e9aa3a079

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require File.join(File.dirname(__FILE__), '../external_test_helper.rb') 
require 'tempfile'
require 'enumerator'
require 'scanf'

class TestReadTime < Test::Unit::TestCase
  include Benchmark

  attr_reader :path, :ints
  
  def setup
    t = Tempfile.new('integers')
    ints = [*(1..100000).to_a]
    t << ints.pack("I*")
    t.close
    @path = t.path
    
    @ints = []
    ints.each_slice(1) do |s|
      @ints << s
    end
  end
  
  def test_read_time
    file = File.open(path, "r")
    results = nil

    benchmark_test(20) do |x|
      x.report("10x full (1)") do 
        (10).times do 
          file.pos = 0
          str = file.read
          results = []
          str.unpack("I*").each_slice(1) do |s|
            results << s
          end
        end 
      end
      assert_equal ints, results
      
      x.report("10x singles (1)") do 
        (10).times do 
          file.pos = 0
          
          results = []
          ints.length.times do
            results << file.read(4).unpack("I")
          end
        end 
      end
      assert_equal ints, results
    end   
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
external-0.1.0 test/benchmarks/test_read_time.rb