import std.stdio, std.datetime, std.array, core.stdc.stdlib, core.exception;
enum NUM_RECORDS = 10_000_000;
__gshared JavaMemoryTrade1[] trades1;
__gshared JavaMemoryTrade2[] trades2;
static assert(JavaMemoryTrade1.sizeof == 20);
static assert(JavaMemoryTrade2.sizeof == 24);
align(4) struct JavaMemoryTrade1 {
private long price, quantity;
private char side;
public long getPrice() const pure nothrow {
return price;
}
public void setPrice(in long price) pure nothrow {
this.price = price;
}
public long getQuantity() const pure nothrow {
return quantity;
}
public void setQuantity(in long quantity) pure nothrow {
this.quantity = quantity;
}
public char getSide() const pure nothrow {
return side;
}
public void setSide(in char side) pure nothrow {
this.side = side;
}
}
struct JavaMemoryTrade2 {
private long tradeId, clientId;
private uint venueCode, instrumentCode;
public long getTradeId() const pure nothrow {
return tradeId;
}
public void setTradeId(in long tradeId) pure nothrow {
this.tradeId = tradeId;
}
public long getClientId() const pure nothrow {
return clientId;
}
public void setClientId(in long clientId) pure nothrow {
this.clientId = clientId;
}
public uint getVenueCode() const pure nothrow {
return venueCode;
}
public void setVenueCode(in uint venueCode) pure nothrow {
this.venueCode = venueCode;
}
public uint getInstrumentCode() const pure nothrow {
return instrumentCode;
}
public void setInstrumentCode(in uint instrumentCode) pure nothrow {
this.instrumentCode = instrumentCode;
}
}
uint pack(in char[]value) pure nothrow {
uint result = 0;
switch (value.length) {
case 4: result = value[3];
goto case;
case 3: result |= (cast(uint)value[2] << 8);
goto case;
case 2: result |= (cast(uint)value[1] << 16);
goto case;
case 1: result |= (cast(uint)value[0] << 24);
break;
default:
throw new Error("Invalid array size");
}
return result;
}
T[] allocArray(T)(in size_t len) nothrow {
auto ptr = cast(T*)malloc(len * T.sizeof);
if (ptr == null)
throw new OutOfMemoryError();
return ptr[0 .. len];
}
void init() nothrow {
trades1 = allocArray!JavaMemoryTrade1(NUM_RECORDS);
trades2 = allocArray!JavaMemoryTrade2(NUM_RECORDS);
immutable char[] londonStockExchange = ['X', 'L', 'O', 'N'];
immutable uint venueCode = pack(londonStockExchange);
immutable char[] billiton = ['B', 'H', 'P'];
immutable uint instrumentCode = pack(billiton);
foreach (i, ref t; trades1) {
t.setPrice(i);
t.setQuantity(i);
t.setSide((i & 1) == 0 ? 'B' : 'S');
}
foreach (i, ref t; trades2) {
t.setTradeId(i);
t.setClientId(1);
t.setVenueCode(venueCode);
t.setInstrumentCode(instrumentCode);
}
}
void perfRun(in uint runNum) {
immutable start = Clock.currSystemTick();
init();
long buyCost = 0, sellCost = 0;
foreach (ref t; trades1)
if (t.getSide() == 'B')
buyCost += t.getPrice() * t.getQuantity();
else
sellCost += t.getPrice() * t.getQuantity();
immutable duration = (Clock.currSystemTick() - start).msecs;
writeln(runNum, " - duration ", duration, "ms");
writeln("buyCost = ", buyCost, " sellCost = ", sellCost);
}
void main() {
foreach (i; 0 .. 5) {
free(trades2.ptr);
free(trades1.ptr);
perfRun(i);
}
}