-
Notifications
You must be signed in to change notification settings - Fork 8
/
C2.c
67 lines (54 loc) · 1.35 KB
/
C2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define NUM_RECORDS (50 * 1000 * 444)
struct CMemoryTrade {
long TradeId; long ClientId; int VenueCode; int InstrumentCode; long Price; long Quantity; char Side;
};
struct CMemoryTrade trades[NUM_RECORDS];
void initTrades() {
for (long i = 0; i < NUM_RECORDS; i++) {
trades[i].TradeId = i;
trades[i].ClientId = 1;
trades[i].VenueCode = 123;
trades[i].InstrumentCode = 321;
trades[i].Price = i;
trades[i].Quantity = i;
if ((i&1) == 0) {
trades[i].Side = 'B';
} else {
trades[i].Side = 'S';
}
}
}
double getTime(){
struct timespec spec;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec);
double s = spec.tv_sec;
double ms = spec.tv_nsec;
return (s*1000 + ms / 1000000);
}
void perfRun(int runNum) {
double startT = getTime();
initTrades();
long buyCost = 0;
long sellCost = 0;
for (long i = 0; i < NUM_RECORDS; i++) {
if (trades[i].Side == 'B') {
buyCost += trades[i].Price * trades[i].Quantity;
} else {
sellCost += trades[i].Price * trades[i].Quantity;
}
}
double endT = getTime();
double duration = endT - startT;
printf("%d - duration %d ms\n", runNum, (int)duration);
printf("buyCost = %ld sellCost = %ld\n", buyCost, sellCost);
}
int main() {
for (int i = 0; i < 5; i++) {
perfRun(i);
}
}