-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.rb
72 lines (51 loc) · 2.48 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
# rubocop:disable all
require 'bundler/setup'
require 'benchmark/ips'
Benchmark::IPS.options[:format] = 'not human'
Benchmark.ips do |x|
# ========================================
# Standard Ruby method
# ========================================
def cash_flow(cash_in, cash_out, reinvest_rate, term, year)
(cash_in - cash_out) * ((1.0 + (reinvest_rate / 100.0))**(term - year))
end
x.report("ruby method") { cash_flow(10000, 800, 50, 30, 5) }
# ========================================
# Rust using Helix as an instance method
# ========================================
require 'cash_flow_rust_helix'
rc = RustCalculations.new
raise 'rust helix instance fail' unless cash_flow(10000, 800, 50, 30, 5) == rc.cash_flow(10000, 800, 50, 30, 5)
x.report("rust helix instance") { rc.cash_flow(10000, 800, 50, 30, 5) }
# ========================================
# Rust using Helix as a class method
# ========================================
require 'cash_flow_rust_helix'
raise 'rust helix class method fail' unless cash_flow(10000, 800, 50, 30, 5) == RustCalculations.cash_flow_class(10000, 800, 50, 30, 5)
x.report("rust helix class") { RustCalculations.cash_flow_class(10000, 800, 50, 30, 5) }
# ========================================
# Rust using FFI class method
# ========================================
require 'ffi'
load 'crates/cash_flow_rust_ffi/src/cash_flow_rust_ffi.rb'
x.report("rust ffi class") { RustFFI.cash_flow_class(10000, 800, 50, 30, 5) }
# ========================================
# Rust using Ruru class method
# ========================================
require 'fiddle'
library = Fiddle::dlopen('crates/cash_flow_rust_ruru/ruby/pair/native/libcash_flow_rust_ruru.dylib')
Fiddle::Function.new(library['initialize'], [], Fiddle::TYPE_VOIDP).call
x.report("rust ruru class") { RustRuru.cash_flow(10000.0, 800.0, 50.0, 30, 5) }
# ========================================
# Rust using rutie class method
# ========================================
require 'cash_flow_rust_rutie'
raise 'rust rutie class method fail' unless cash_flow(10000, 800, 50, 30, 5) == RustRutie.cash_flow(10000.0, 800.0, 50.0, 30, 5)
x.report("rust rutie class") { RustRutie.cash_flow(10000.0, 800.0, 50.0, 30, 5) }
# ====================================
# C using standard Ruby extension
# ====================================
require 'cash_flow_c'
x.report("c class") { CCalculations.cash_flow(10000, 800, 50, 30, 5) }
x.compare!
end