Skip to content

Commit 10731b2

Browse files
author
os6sense
committed
A few optimisations: symbols, times for loops,GC
1 parent 99e67ef commit 10731b2

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Ruby1.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
NUM_RECORDS = 50 * 1000 * 444
2+
3+
class RubyMemTrade
4+
attr_reader :tId, :cId, :vCode, :iCode, :price, :quantity, :side
5+
6+
def initialize(tId, cId, vCode, iCode, price, quantity, side)
7+
@tradeId=tId
8+
@clientId=cId
9+
@venueCode=vCode
10+
@instrumentCode=iCode
11+
@price=price
12+
@quantity=quantity
13+
@side=side
14+
end
15+
def fromI(i)
16+
@clientId=1
17+
@venueCode=123
18+
@instrumentCode=321
19+
@price = @quantity = @tradeId = i
20+
@side = i.even? ? :B : :S
21+
end
22+
end
23+
24+
def prepareTrades(trades)
25+
NUM_RECORDS.times { |i| trades << RubyMemTrade.new(0,0,0,0,0,0, :a) }
26+
end
27+
28+
def initTrades(trades)
29+
NUM_RECORDS.times { |i| trades[i].fromI(i) }
30+
end
31+
32+
def perfRun(trades, runNum)
33+
GC.disable
34+
startT = Time.now()
35+
initTrades(trades)
36+
buyCost = sellCost = 0
37+
trades.each do | trade |
38+
if trade.side == :B
39+
buyCost += trade.price * trade.quantity
40+
else
41+
sellCost += trade.price * trade.quantity
42+
end
43+
end
44+
45+
endT = Time.now()
46+
duration = (endT - startT) * 1000
47+
printf("%d - duration %d ms\n", runNum, duration)
48+
printf("buyCost = %d sellCost = %d\n", buyCost, sellCost)
49+
GC.enable
50+
GC.start
51+
end
52+
53+
if __FILE__ == $0
54+
trades = []
55+
prepareTrades(trades)
56+
for i in 0..5
57+
perfRun(trades, i)
58+
end
59+
end
60+

0 commit comments

Comments
 (0)