Skip to content

Instantly share code, notes, and snippets.

@mratsim
Created May 24, 2017 21:13
Show Gist options
  • Save mratsim/dc4af77536e21f6cd716e6c9950e2373 to your computer and use it in GitHub Desktop.
Save mratsim/dc4af77536e21f6cd716e6c9950e2373 to your computer and use it in GitHub Desktop.
## main.nim - uses a loop and a split
import strutils
let inFile = open("input.txt", fmRead)
let outFile = open("output.txt", fmWrite)
var ln: TaintedString = ""
var parts: seq[string]
while inFile.readLine(ln):
parts = ln.split('\t')
for idx, val in pairs(parts):
parts[idx] &= "_mark"
outFile.writeLine(parts.join(", "))
outFile.close()
inFile.close()
## main_sequtils.nim - uses sequtils
import strutils, sequtils
let inFile = open("input.txt", fmRead)
let outFile = open("output.txt", fmWrite)
var ln: TaintedString = ""
while inFile.readLine(ln):
outFile.writeLine(map(ln.split('\t'), proc(x: string): string = x & "_mark").join(", "))
outFile.close()
inFile.close()
## main_lines.nim - uses the lines() proc
import strutils
let
inFile = open("input.txt", fmRead)
outFile = open("output.txt", fmWrite)
var parts: seq[string]
for ln in inFile.lines:
parts = ln.split('\t')
for idx, val in pairs(parts):
parts[idx] &= "_mark"
outFile.writeLine(parts.join(", "))
outFile.close()
inFile.close()
## main_streams.nim - uses the streams module
import strutils, streams
let
inFile = newFileStream("input.txt", fmRead)
outFile = open("output.txt", fmWrite)
var
parts: seq[string]
ln = ""
while inFile.readLine(ln):
parts = ln.split('\t')
for idx, val in pairs(parts):
parts[idx] &= "_mark"
outFile.writeLine(parts.join(", "))
outFile.close()
inFile.close()
## main_streams_sequtils - uses the streams module, with sequtils
import strutils, streams, sequtils
let
inFile = newFileStream("input.txt", fmRead)
outFile = open("output.txt", fmWrite)
var ln = ""
while inFile.readLine(ln):
outFile.writeLine(map(ln.split('\t'), proc(x: string): string = x & "_mark").join(", "))
outFile.close()
inFile.close()
## Fast
import streams, parsecsv, strutils
proc main() =
let
fname = "input.txt"
infile = newFileStream(fname, fmRead)
outfile = open("output.txt", fmWrite)
var
vcf: CsvParser
open(vcf, infile, fname, separator='\t')
while readRow(vcf):
for i, word in vcf.row:
vcf.row[i] = word & "_mark"
outfile.writeLine(join(vcf.row, ", "))
main()
### Stats 1GB
➜ test_nim_file time python3 ./main.py
python3 ./main.py 8.81s user 1.58s system 98% cpu 10.558 total
➜ test_nim_file time ./main
./main 3.76s user 1.50s system 96% cpu 5.451 total
➜ test_nim_file time ./main_sequtils
./main_sequtils 3.63s user 1.46s system 96% cpu 5.279 total
➜ test_nim_file time ./main_lines
./main_lines 3.98s user 1.49s system 98% cpu 5.572 total
➜ test_nim_file time ./main_streams
./main_streams 50.99s user 1.73s system 99% cpu 53.041 total
➜ test_nim_file time ./main_streams_sequtils
./main_streams_sequtils 50.95s user 1.73s system 99% cpu 53.002 total
➜ test_nim_file
### Stats 100MB
nim-parse:master$ make time
for b in nimer nvill nvill_unwrapped euant0 euant_lines euant_streams euant_sequtils euant_streams_sequtils; do echo $b; time -p ./$b; done
nimer
real 2.23
user 1.91
sys 0.30
nvill
real 1.92
user 1.60
sys 0.29
nvill_unwrapped
real 2.82
user 2.51
sys 0.29
euant0
real 2.14
user 1.77
sys 0.34
euant_lines
real 2.03
user 1.70
sys 0.30
euant_streams
real 8.28
user 7.94
sys 0.30
euant_sequtils
real 2.29
user 1.95
sys 0.32
euant_streams_sequtils
real 8.49
user 8.16
sys 0.30
time -p python2.7 quick.py
real 4.63
user 4.30
sys 0.30
time -p python3 quick.py
real 5.07
user 4.77
sys 0.24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment