Skip to content

Instantly share code, notes, and snippets.

@kojix2
Last active January 1, 2019 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kojix2/9d22fecf8c7c5f304c2311f005b7ecc1 to your computer and use it in GitHub Desktop.
Save kojix2/9d22fecf8c7c5f304c2311f005b7ecc1 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'matrix'
require 'numo/narray'
require 'cumo'
SIZE = 2000
NUM = 1000
Xumo = [Numo, Cumo]
Types = %i[UInt8 Int32 SFloat]
arithmetic = [
'z = x + y',
'z = x - y',
'z = x * y',
'z = x / y'
]
Benchmark.bm 30 do |r|
Xumo.each do |xumo|
Types.each do |type|
narray = xumo.const_get(type)
x = narray.new(SIZE, SIZE).seq
y = narray.new(SIZE, SIZE).seq.reverse
x[x.eq 0] = 1
y[y.eq 0] = 1
arithmetic.each do |s|
str = x.class.to_s.ljust(16) + s
r.report str do
NUM.times do
eval(s)
end
end
end
end
end
x = Matrix.build(SIZE, SIZE) { |row, col| SIZE * row + col }
y = Matrix.build(SIZE, SIZE) { |row, col| SIZE * (SIZE - 1 - row) + (SIZE - 1 - col) }
arithmetic.each do |s|
str = x.class.to_s.ljust(16) + s
r.report str do
NUM.times do
eval(s)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment