File: benchmark_dvector_reads.rb

package info (click to toggle)
ruby-tioga 1.14-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,508 kB
  • sloc: ansic: 39,112; ruby: 17,031; sh: 79; makefile: 29
file content (90 lines) | stat: -rw-r--r-- 2,138 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# A small benchmarking file for fancy_read and fast_fancy_read, just to make
# sure the latter deserves its name ;-)...
#
# Now the comparison between fancy_read and fast_fancy_read 

require 'Dobjects/Dvector'
require 'benchmark'
require 'stringio'
require 'tempfile'

Benchmark.bm do |x|
  # We first create a 'dummy file':
  f = Tempfile.new("data")
  x.report("data writing(100000):") do 
    100000.times do |i|
      f.puts "#{i*1.0}\t#{i**1.3}\t#{i**0.7}"
    end
    f.close
  end
  x.report("fancy_read(100 000):") do 
    stream = File.open(f.path)
    Dobjects::Dvector.fancy_read(stream, nil, 'default'=> 0.0).size
  end
  x.report("fancy_read(100 000), 2nd time:") do 
    stream = File.open(f.path)
    Dobjects::Dvector.fancy_read(stream, nil, 'default'=> 0.0).size
  end

  # Trying pre-allocation.
  x.report("fancy_read(100 000), preallocation to 1 :") do 
    stream = File.open(f.path)
    Dobjects::Dvector.fancy_read(stream, nil, 'default'=> 0.0, 
                                 'initial_size' => 1).size
  end
  x.report("fancy_read(100 000), preallocation to 49999 :") do 
    stream = File.open(f.path)
    Dobjects::Dvector.fancy_read(stream, nil, 'default'=> 0.0, 
                                 'initial_size' => 49999).size
  end
  x.report("fancy_read(100 000), preallocation to 100 000 :") do 
    stream = File.open(f.path)
    Dobjects::Dvector.fancy_read(stream, nil, 'default'=> 0.0, 
                                 'initial_size' => 100000).size
  end

  # We create a smaller file:
  f = Tempfile.new("data")
  1000.times do |i|
    f.puts "#{i*1.0}\t#{i**1.3}\t#{i**0.7}"
  end
  f.close
  x.report("fancy_read(100 * 1000):") do 
    100.times do 
      stream = File.open(f.path)
      Dobjects::Dvector.fancy_read(stream, nil, 'default'=> 0.0)
    end
  end

  # We use a StringIO
  string = ""
  x.report("string creation:") do 
    50000.times do |i|
      string.concat("#{i*1.0}\t#{i**1.3}\t#{i**0.7}\n")
    end
  end
  x.report("fancy_read(50000):") do 
    stream = StringIO.new(string)
    Dobjects::Dvector.fancy_read(stream, nil, 'default'=> 0.0).size
  end

end