-
Notifications
You must be signed in to change notification settings - Fork 8
/
C3.c
74 lines (60 loc) · 1.47 KB
/
C3.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
68
69
70
71
72
73
74
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <gmp.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();
mpz_t buyCost;
mpz_init(buyCost);
mpz_t sellCost;
mpz_init(sellCost);
mpz_t toAdd;
mpz_init(toAdd);
for (long i = 0; i < NUM_RECORDS; i++) {
mpz_set_si(toAdd, trades[i].Price * trades[i].Quantity);
if (trades[i].Side == 'B') {
mpz_add(buyCost, buyCost, toAdd);
} else {
mpz_add(sellCost, sellCost, toAdd);
}
}
double endT = getTime();
double duration = endT - startT;
printf("%d - duration %d ms\n", runNum, (int)duration);
gmp_printf("buyCost = %Zd sellCost = %Zd\n", buyCost, sellCost);
}
int main() {
for (int i = 0; i < 5; i++) {
perfRun(i);
}
}