Last active
August 29, 2015 13:56
-
-
Save logicchains/8882549 to your computer and use it in GitHub Desktop.
Go language implementation of the code at http://mechanical-sympathy.blogspot.com.au/2012/10/compact-off-heap-structurestuples-in.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
const ( | |
NUM_RECORDS = 50 * 1000 * 1000 | |
) | |
var ( | |
trades [NUM_RECORDS]GoMemoryTrade | |
) | |
type GoMemoryTrade struct { | |
TradeId int64 | |
ClientId int64 | |
VenueCode int32 | |
InstrumentCode int32 | |
Price int64 | |
Quantity int64 | |
Side byte | |
} | |
func main() { | |
for i := 0; i < 5; i++ { | |
perfRun(i) | |
} | |
} | |
func perfRun(runNum int) { | |
start := time.Now() | |
initTrades() | |
var m runtime.MemStats | |
runtime.ReadMemStats(&m) | |
fmt.Printf("Memory %v total, %v free\n", | |
m.TotalAlloc, | |
m.TotalAlloc-m.Alloc) | |
var buyCost int64 | |
var sellCost int64 | |
var i int64 | |
for ; i < NUM_RECORDS; i++ { | |
trade := &(trades[i]) | |
if (*trade).Side == 'B' { | |
buyCost += (*trade).Price * (*trade).Quantity | |
} else { | |
sellCost += (*trade).Price * (*trade).Quantity | |
} | |
} | |
endT := time.Now() | |
duration := endT.Sub(start).Nanoseconds() / 1000000 | |
fmt.Printf("%v - duration %v ms\n", runNum, duration) | |
fmt.Printf("buyCost = %v sellCost = %v\n", buyCost, sellCost) | |
} | |
func initTrades() { | |
londonStockExchange := []byte{'X', 'L', 'O', 'N'} | |
venueCode := pack(londonStockExchange) | |
billiton := []byte{'B', 'H', 'P'} | |
instrumentCode := pack(billiton) | |
var i int64 | |
for ; i < NUM_RECORDS; i++ { | |
trade := &(trades[i]) | |
(*trade).TradeId = i | |
(*trade).ClientId = 1 | |
(*trade).VenueCode = venueCode | |
(*trade).InstrumentCode = instrumentCode | |
(*trade).Price = i | |
(*trade).Quantity = i | |
if (i&1) == 0 { | |
(*trade).Side = 'B' | |
} else { | |
(*trade).Side = 'S' | |
} | |
} | |
} | |
func pack(value []byte) int32 { | |
var result int32 | |
switch len(value) { | |
case 4: | |
result = int32(value[3]) | |
fallthrough | |
case 3: | |
result |= int32(value[2]) << 8 | |
fallthrough | |
case 2: | |
result |= int32(value[1]) << 16 | |
fallthrough | |
case 1: | |
result |= int32(value[0]) << 24 | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment