Hello Nim users!
Just discovered Nim and have been playing with it.
Can you tell me what I am doing wrong here? Because Python seems to be much
_faster_ than Nim with this code?
This python code is executed to a text file with 3.6MB in size, some 43000
lines. (sorry, can not publish the file)
* * *
import sys
import time;
# Usage: python test.py <filename.txt> "<what is replaced>" "<replaced
with this>" """
ms_1 = time.time()*1000.0
sFile, sFind, sReplaced = sys.argv[1], sys.argv[2], sys.argv[3]
fp = open(sFile.replace(".", "_new."), "w")
sFind = sFind.replace("\"", "")
for sLine in open(sFile):
if len(sLine) > 2:
if sLine.find(sFind) > -1:
fp.write(sLine.replace(sFind, sReplaced.replace("\"", "")))
else:
fp.write(sLine)
else:
fp.write(sLine)
print "\nTook: " , (time.time()*1000.0 - ms_1), " ms"
Run
* * *
vs. the same code in Nim:
* * *
import os, times, strutils
#Compile: nim --passc:-flto --opt:size c test.nim
let lstParams = commandLineParams()
var
flDurat: float = 0.0
sFind: string = lstParams[1]
sLine: string = ""
let
sFile: string = lstParams[0]
sReplaced: string = lstParams[2]
flTime = cpuTime()
let f2 = open(sFile.replace(".", "_new."), fmWrite)
sFind = sFind.replace("\"", "")
let f = open(sFile)
while f.readLine(sLine):
if len(sLine) > 2:
if sLine.find(sFind) > -1:
f2.writeLine(sLine.replace(sFind, sReplaced.replace("\"", "")))
else:
f2.writeLine(sLine)
else:
f2.writeLine(sLine)
flDurat = (cpuTime() - flTime)
close(f)
echo "\nReplace took: ", flDurat, " s" # --> 0.249 s
Run
* * *
==> results:
Python: Took: 78.0 ms
Nim: Replace took: 0.24 s
Run
So Python seems to be 3 x faster?
Same result if I use getTime().nanosecond for calculation.
I have Nim Compiler Version 1.0.6 [Windows: amd64]