[ create a new paste ] login | about

Link: http://codepad.org/IhHjqUua    [ raw code | fork ]

D, pasted on Oct 26:
import std.stdio, std.datetime, std.array;

private enum int NUM_RECORDS = 10_000_000;

__gshared JavaMemoryTrade[] trades;

align(4) struct JavaMemoryTrade {
    private int venueCode, instrumentCode;
    private long tradeId, clientId, price, quantity;
    private char side;

    public long getTradeId() 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 int getVenueCode() const pure nothrow {
        return venueCode;
    }

    public void setVenueCode(in int venueCode) pure nothrow {
        this.venueCode = venueCode;
    }

    public int getInstrumentCode() const pure nothrow {
        return instrumentCode;
    }

    public void setInstrumentCode(in int instrumentCode) pure nothrow {
        this.instrumentCode = instrumentCode;
    }

    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;
    }
}

private int pack(in char[] value) pure {
    int result = 0;

    switch (value.length) {
        case 4: result =            value[3];
        case 3: result |= (cast(int)value[2] <<  8);
        case 2: result |= (cast(int)value[1] << 16);
        case 1: result |= (cast(int)value[0] << 24);
                break;
        default: throw new Error("Invalid array size");
    }

    return result;
}

void init() {
    trades = minimallyInitializedArray!(JavaMemoryTrade[])(NUM_RECORDS);

    immutable char[] londonStockExchange = ['X', 'L', 'O', 'N'];
    immutable venueCode = pack(londonStockExchange);

    immutable char[] billiton = ['B', 'H', 'P'];
    immutable instrumentCode = pack(billiton);

    foreach (i, ref trade; trades) {
        trade.setTradeId(i);
        trade.setClientId(1);
        trade.setVenueCode(venueCode);
        trade.setInstrumentCode(instrumentCode);
        trade.setPrice(i);
        trade.setQuantity(i);
        trade.setSide((i & 1) == 0 ? 'B' : 'S');
    }
}

void perfRun(in int runNum) {
    auto start = Clock.currSystemTick();

    init();
    long buyCost = 0;
    long sellCost = 0;

    foreach (ref trade; trades)
        if (trade.getSide() == 'B')
            buyCost += trade.getPrice() * trade.getQuantity();
        else
            sellCost += trade.getPrice() * trade.getQuantity();

    immutable duration = (Clock.currSystemTick() - start).msecs;
    writeln(runNum, " - duration ", duration, "ms");
    writeln("buyCost = ", buyCost, " sellCost = ", sellCost);
}

void main() {
    for (int i = 0; i < 5; i++) {
        delete trades;
        perfRun(i);
    }
}


Create a new paste based on this one


Comments: