|
| 1 | +let [<Literal>] NUM_RECORDS = |
| 2 | + // F# didn't support constant folding until F# 3.1, so hard-code this constant for now. |
| 3 | + 22200000 // 50 * 1000 * 444 |
| 4 | + |
| 5 | +[<Struct>] |
| 6 | +type FSMemTrade = |
| 7 | + val mutable TradeId : int64; |
| 8 | + val mutable ClientId : int64; |
| 9 | + val mutable VenueId : int32; |
| 10 | + val mutable InstrumentCode : int32; |
| 11 | + val mutable Price : int64; |
| 12 | + val mutable Quantity : int64; |
| 13 | + val mutable Side : char |
| 14 | + |
| 15 | +let trades = |
| 16 | + Array.init NUM_RECORDS <| fun i -> |
| 17 | + // In C# and F#, struct fields are initialized by default to the 'default' value for the field type; |
| 18 | + // this means you only need to set fields which need to be set to a non-default value. |
| 19 | + FSMemTrade ( |
| 20 | + Side = 'a') |
| 21 | + |
| 22 | +let initTrades () = |
| 23 | + printfn "initiating trades" |
| 24 | + |
| 25 | + for i = 0 to NUM_RECORDS - 1 do |
| 26 | + trades.[i] <- |
| 27 | + let long_i = int64 i |
| 28 | + FSMemTrade ( |
| 29 | + TradeId = long_i, |
| 30 | + ClientId = 1L, |
| 31 | + VenueId = 123, |
| 32 | + InstrumentCode = 321, |
| 33 | + Price = long_i, |
| 34 | + Quantity = long_i, |
| 35 | + Side = if (i % 2 = 0) then 'S' else 'B') |
| 36 | + |
| 37 | +let perfRun n = |
| 38 | + let mutable buyCost = 0L |
| 39 | + let mutable sellCost = 0L |
| 40 | + |
| 41 | + let watch = System.Diagnostics.Stopwatch.StartNew () |
| 42 | + |
| 43 | + initTrades () |
| 44 | + |
| 45 | + for i = 0 to NUM_RECORDS - 1 do |
| 46 | + let cost = trades.[i].Price * trades.[i].Quantity |
| 47 | + if trades.[i].Side = 'B' then |
| 48 | + buyCost <- buyCost + cost |
| 49 | + else |
| 50 | + sellCost <- sellCost + cost |
| 51 | + |
| 52 | + watch.Stop () |
| 53 | + |
| 54 | + let duration = int watch.Elapsed.TotalMilliseconds |
| 55 | + printfn "%d - duration %d ms" n duration |
| 56 | + printfn "buyCost = %O sellCost = %O" buyCost sellCost |
| 57 | + |
| 58 | +[<EntryPoint>] |
| 59 | +let main args = |
| 60 | + for i = 0 to 4 do |
| 61 | + System.GC.Collect () |
| 62 | + perfRun i |
| 63 | + |
| 64 | + 0 // Exit code |
0 commit comments