Hey there,

This would be an idiomatic Nim translation of your program (and the result is 
more similar to the Python one).
    
    
    import os, strutils, times, math, parsecsv, streams
    
    const CsvPath = "./build/EUR_USD_Week1.csv"
    # Row: lTid,cDealable,CurrencyPair,RateDateTime,RateBid,RateAsk
    
    proc main() =
      
      var ctime = getTime()
      echo("time: ", ctime)
      var cpustart = cpuTime()
      
      var csv: CsvParser
      let stream = newFileStream(CsvPath, mode = fmRead)
      csv.open( stream, CsvPath,
                separator = ',',
                quote = '\"',
                skipInitialSpace = true
              )
      defer: csv.close
      
      var
        psum = 0.0
        pcount = -1
        pips = 0.0
        psumsum = 0.0
        # Preallocating the seq.
        # Plain arrays are allocated on the stack and
        # stack size is very limited (a couple MB)
        # Use seq or ref arrays instead
        farray = newSeq[float](337588)
      
      discard csv.readRow # Skip header row
      
      while csv.readRow():
        let price = csv.row[5].parseFloat
        pcount += 1
        farray[pcount] = ((price * price) * ((pcount+1) / farray.len))
        pips += price
        psum = farray.sum()
        psumsum += psum
      
      echo("pcount: ", $pcount, "  pips: ", $pips, "  farray[^1]: ", 
farray[^1], "  psum: ", $psum, "  psumsum: ", $psumsum)
      echo("clock:  " & $(getTime()-ctime) & "   cpuTime: " & 
$(cpuTime()-cpustart))
    
    main()
    
    
    Run

Now this idiomatic Nim is still 2.3x slower than Python but I'll have a look at 
the performance issue later.

Also for your csv processing (and tabular data in general) I suggest you use 
Python [Pandas](https://pandas.pydata.org) and 
[NimData](https://github.com/bluenote10/NimData) they are made for this.

Reply via email to