Last active
August 29, 2015 13:56
-
-
Save logicchains/8883534 to your computer and use it in GitHub Desktop.
C 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
#define _POSIX_C_SOURCE 200809L | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <math.h> | |
#define NUM_RECORDS (50 * 1000 * 1000) | |
struct CMemoryTrade { | |
long TradeId; long ClientId; int VenueCode; int InstrumentCode; long Price; long Quantity; char Side; | |
}; | |
struct CMemoryTrade trades[NUM_RECORDS]; | |
int pack(char* value, int len) { | |
int result = 0; | |
switch (len) { | |
case 4: | |
result = (int)value[3]; | |
case 3: | |
result |= (int)value[2] << 8; | |
case 2: | |
result |= (int)value[1] << 16; | |
case 1: | |
result |= (int)value[0] << 24; | |
break; | |
} | |
return result; | |
} | |
void initTrades() { | |
char* londonStockExchange; | |
londonStockExchange = (char[4]){'X', 'L', 'O', 'N'}; | |
int venueCode = pack(londonStockExchange,4); | |
free(londonStockExchange); | |
char* billiton; | |
billiton = (char[3]){'B', 'H', 'P'}; | |
int instrumentCode = pack(billiton,3); | |
free(billiton); | |
for (long i = 0; i < NUM_RECORDS; i++) { | |
struct CMemoryTrade *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'; | |
} | |
} | |
} | |
double getTime(){ | |
struct timespec spec; | |
clock_gettime(CLOCK_REALTIME, &spec); | |
double s = spec.tv_sec; | |
double ms = spec.tv_nsec; | |
return (s*1000 + ms / 1000000); | |
} | |
void perfRun(int runNum) { | |
double startT = getTime(); | |
initTrades(); | |
// var m runtime.MemStats This is the Go code; not sure how to easily get this information in C. | |
// runtime.ReadMemStats(&m) | |
// fmt.Printf("Memory %v total, %v free\n", | |
// m.TotalAlloc, | |
// m.TotalAlloc-m.Alloc) | |
long buyCost = 0; | |
long sellCost = 0; | |
for (long i = 0; i < NUM_RECORDS; i++) { | |
struct CMemoryTrade *trade = &(trades[i]); | |
if (trade->Side == 'B') { | |
buyCost += trade->Price * trade->Quantity; | |
} else { | |
sellCost += trade->Price * trade->Quantity; | |
} | |
} | |
double endT = getTime(); | |
double duration = endT - startT; | |
printf("%d - duration %f ms\n", runNum, duration); | |
printf("buyCost = %ld sellCost = %ld\n", buyCost, sellCost); | |
} | |
int main() { | |
printf("%ld",sizeof(struct CMemoryTrade)); | |
for (int i = 0; i < 5; i++) { | |
perfRun(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment