-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_parse_bar_stripped.c
More file actions
121 lines (121 loc) · 3.49 KB
/
ex_parse_bar_stripped.c
File metadata and controls
121 lines (121 loc) · 3.49 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
typedef char TimeStr[24];
typedef char DayStr[5];
double CLOCKS_PER_MS = CLOCKS_PER_SEC / 1000.0;
double nowms() {
return (((double) clock()) / CLOCKS_PER_MS);
//return ((double) clock() /);
}
double elap(const char *msg, double start, double stop) {
double tmp = stop - start;
printf ("%s elap = %9.2f\n", msg, tmp);
}
char * copyNext(char *dest, char *src, char delim) {
while ((*src != 0) && (*src != delim)) {
*dest = *src;
dest++;
src++;
}
*dest = 0;
if (*src != 0)
src++;
return src;
}
double avg(double *nptr, int numCnt) {
double total = 0.0;
for (int ndx=0; ndx < numCnt; ndx++)
{
total += nptr[ndx];
}
return total / numCnt;
}
int main() {
double startRun = nowms();
FILE *ptr_file;
char buf[1000];
char bbuf[50003];
ptr_file =fopen("2014.M1.csv","r");
if (!ptr_file)
return 1;
double startCount = nowms();
int bcnt = 0;
int numRec = 0;
while (1) {
int numRead = fread(bbuf,1, 50000, ptr_file);
if (numRead == 0)
break;
bbuf[numRead+1] = 0;
char *ptr = (char *) &bbuf;
for (; *ptr != 0; ptr++) {
if (*ptr == '\n')
numRec++;
}
bcnt++;
}
numRec--; // take one away for header.
double stopCount = nowms();
int lca = numRec + 1;
double startAlloc = nowms();
TimeStr *vtimes = (TimeStr *) malloc(sizeof(TimeStr) * lca);
DayStr *vdays = (DayStr *) malloc(sizeof(DayStr) * lca);
double *vopen = (double *) malloc(sizeof(double) * lca);
double *vclose =(double *) malloc(sizeof(double) * lca);
double *vhigh = (double *) malloc(sizeof(double) * lca);
double *vlow = (double *) malloc(sizeof(double) * lca);
int *vvolume = (int *) malloc(sizeof(int) * lca);
double stopAlloc = nowms();
double startBuild = nowms();
fseek(ptr_file, 0, SEEK_SET);
int lc = 0;
char * tkn;
char tkbuf[80];
fgets(buf,1000, ptr_file); // read first line
while (fgets(buf,1000, ptr_file)!=NULL) {
double tnum;
tkn = (char *) &buf;
tkn = copyNext(vtimes[lc], tkn, ','); // dateTime
tkn = copyNext(vdays[lc], tkn, ','); // day
tkn = copyNext(tkbuf, tkn, ','); // open
vopen[lc] = atof((char *) &tkbuf);
tkn = copyNext(tkbuf, tkn, ','); // close
vclose[lc] = atof((char *) &tkbuf);
tkn = copyNext(tkbuf, tkn, ','); // high
vhigh[lc] = atof((char *) &tkbuf);
tkn = copyNext(tkbuf, tkn, ','); // low
vlow[lc] = atof((char *) &tkbuf);
tkn = copyNext(tkbuf, tkn, ','); // volume
vvolume[lc] = atol((char *) &tkbuf);
lc++;
}
lc--; // one to high at end.
double stopBuild = nowms();
double stopRun = stopBuild;
double startAvg = nowms();
double tavg = avg(vclose, lc);
double stopAvg = nowms();
double start100Avg = nowms();
for (int x=0; x<=100; x++)
tavg = avg(vclose, lc);
double stop100Avg = nowms();
double startCleanup = nowms();
free(vopen);
free(vclose);
free(vhigh);
free(vlow);
fclose(ptr_file);
double stopCleanup = nowms();
elap("Read in 50K blocks", startCount, stopCount);
printf("Read %d of 50k blocks\n", bcnt);
printf("Read %d lines\n", numRec);
elap("allocate vector array", startAlloc, stopAlloc);
elap("build Recs", startBuild, stopBuild);
printf("Read %d numRec\n", lc);
elap("Total Load ", startRun, stopRun);
elap("calc average", startAvg, stopAvg);
double telap = stop100Avg - start100Avg;
printf("calc average 100 times %11.2f each=%11.2f\n", telap, (telap / 100));
elap("cleanup", startCleanup, stopCleanup);
return 0;
}