-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.rb
129 lines (118 loc) · 2.12 KB
/
benchmark.rb
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env ruby
# encoding: utf-8
Version = '20130410-102621'
ELEMENTS = 10000
TIMES = 1000
require 'benchmark'
def benchmark(a1, a2)
Benchmark.bm(20) do |x|
x.report("Add") do
TIMES.times do
a1 + a2
end
end
x.report("Sub") do
TIMES.times do
a1 - a2
end
end
x.report("Mul") do
TIMES.times do
a1 * a2
end
end
x.report("Div") do
TIMES.times do
a1 / a2
end
end
end
end
# CArray
class Array
def +(other)
clone = self.clone
self.length.times do |i|
self.length.times do |j|
clone[i][j]+=other[i][j]
end
end
clone
end
def -(other)
clone = self.clone
self.length.times do |i|
self.length.times do |j|
clone[i][j]-=other[i][j]
end
end
clone
end
def *(other)
clone = self.clone
self.length.times do |i|
self.length.times do |j|
clone[i][j]*=other[i][j]
end
end
clone
end
def /(other)
clone = self.clone
self.length.times do |i|
self.length.times do |j|
clone[i][j]/=other[i][j]
end
end
clone
end
end
a1 = Array.new(ELEMENTS){|i| i+1.0}
a2 = Array.new(ELEMENTS){|i| i+5.0}
n = Math.sqrt(ELEMENTS)
unless (n.to_i - n).abs < 1e-10
warn "Math.sqrt(ELEMENTS (#{ELEMENTS})) must be an integer."
exit
end
n = n.to_i
m1 = []
m2 = []
m1 << a1.slice!(0,n) while !a1.empty?
m2 << a2.slice!(0,n) while !a2.empty?
# CArray
puts "CArray"
benchmark(m1, m2)
puts
# GSL::Matrix
require 'gsl'
a1 = Array.new(ELEMENTS){|i| i+1.0}
a2 = Array.new(ELEMENTS){|i| i+5.0}
gm1 = GSL::Matrix.alloc(a1, n, n)
gm2 = GSL::Matrix.alloc(a2, n, n)
puts "GSL::Matrix"
benchmark(gm1, gm2)
puts
# NMatrix
#require 'nmatrix'
require 'nmatrix-0.0.3'
nm1 = NMatrix.new([n,n], a1, :float64)
nm2 = NMatrix.new([n,n], a2, :float64)
puts "NMatrix"
benchmark(nm1, nm2)
puts
# NArray
begin
require 'narray'
rescue
end
na1 = NArray.to_na(m1)
na2 = NArray.to_na(m2)
puts "NArray"
benchmark(na1, na2)
puts
# GSL::Matrix.to_nm
nm3 = gm1.to_nm
nm4 = gm2.to_nm
puts "NMatrix from GSL::Matrix (to_nm)"
benchmark(nm3, nm4)
puts