Skip to content

Commit 3331c86

Browse files
author
os6sense
committed
Naive scala implementation.
1 parent a82320a commit 3331c86

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Scala.scala

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*
3+
Run with -J-Xmx2g option to increase heapsize
4+
*/
5+
class ScalaMemTrade(tId: Int, cId: Int, vCode: Int, iCode: Int, iPrice: BigInt, iQuantity: BigInt, sSide: Char) {
6+
7+
var tradeId: Int =tId
8+
var clientId: Int =cId
9+
var venueCode: Int =vCode
10+
var instrumentCode: Int =iCode
11+
var price: BigInt =iPrice
12+
var quantity: BigInt =iQuantity
13+
var side: Char =sSide
14+
15+
def fromI(i: Int) = {
16+
tradeId=i
17+
clientId=1
18+
venueCode=123
19+
instrumentCode=321
20+
price=i
21+
quantity=i
22+
side = if (i % 2 == 0) 'B' else 'S';
23+
}
24+
}
25+
26+
object ScalaTrade {
27+
val NUM_RECORDS: Int = 1 * 1000 * 444;
28+
29+
val trades: Array[ScalaMemTrade] = new Array[ScalaMemTrade](NUM_RECORDS);
30+
31+
def prepareTrades() : Unit = {
32+
var i = 0
33+
while (i < NUM_RECORDS) { trades(i) = new ScalaMemTrade(0,0,0,0,0,0,'a'); i+=1 }
34+
}
35+
36+
def initTrades() : Unit = {
37+
var i = 0
38+
while (i < NUM_RECORDS) { trades(i).fromI(i); i+=1 }
39+
}
40+
41+
def perfRun(runNum: Int): Unit = {
42+
val startT: Long = System.currentTimeMillis()
43+
var i = 0
44+
initTrades()
45+
var buyCost: BigInt = 0
46+
var sellCost: BigInt = 0
47+
while (i < NUM_RECORDS) {
48+
if (trades(i).side == 'B')
49+
buyCost += trades(i).price * trades(i).quantity
50+
else
51+
sellCost += trades(i).price * trades(i).quantity
52+
i += 1
53+
}
54+
val endT: Long = System.currentTimeMillis()
55+
val duration = (endT - startT)
56+
printf("%d - duration %d ms\n", runNum, duration)
57+
printf("buyCost = %d sellCost = %d\n", buyCost, sellCost)
58+
}
59+
60+
def main(args: Array[String]) {
61+
prepareTrades()
62+
System.gc()
63+
(0 to 5).map { i => System.gc(); printf("Run %d\n", i); perfRun(i) }
64+
}
65+
}
66+
67+
ScalaTrade.main(args)

0 commit comments

Comments
 (0)