-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file_line_by_line.nim
62 lines (55 loc) · 1.42 KB
/
read_file_line_by_line.nim
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
import times
import strutils
var fname = "c:\\calgo-data\\exp-EURUSD-Tick-rsi14-stddev100-bars.csv"
var i = open(fname)
var startLoad = cpuTime()
var tcol = newSeq[string](1)
for line in i.lines:
tcol.add(line)
i.close()
var elap = cpuTime() - startLoad
echo "completed read file line by line"
echo elap
startLoad = cpuTime()
for line in lines(fname):
var flds = split(line, ',')
var afld = flds[3]
elap = cpuTime() - startLoad
echo "complete entire file read using lines split each line on comma"
echo elap
startLoad = cpuTime()
var allbytes = readFile(fname)
elap = cpuTime() - startLoad
echo "complete entire file entire file as bytes "
echo elap
var startsplit = cpuTime()
var startline = 0
var endline = 0
# TODO: Add function to parse out the
# the lines by detecting \n and then split
# each of those lines into an array of fields
var numLine = 0
var lineBeg = 0
var lineEnd = 0
var currNdx = 0
var tnums = newSeq[float](1)
for tc in allbytes:
if tc == '\L':
lineEnd = currNdx
if numLine > 0:
var aline = allbytes[lineBeg .. lineEnd]
var fields = aline.split(',')
var fldval = fields[3]
try:
var anum = parseFloat(fldval)
tnums.add(anum)
except ValueError:
echo "value error parsing float"
echo aline
echo fldval
numLine += 1
lineBeg = lineEnd + 1
currNdx += 1
elap = cpuTime() - startsplit
echo "finished split preloaded block"
echo elap