Here is my version, I switched to measuring nano seconds to get numbers 
different from 0:
    
    
    # Compile and run with 'nim c -r -d:useRealtimeGC -d:release main.nim'
    
    import strutils
    #import times
    
    include "$lib/system/timers"
    
    const
      windowSize = 200000
      msgCount   = 1000000
    
    type
      Msg = seq[byte]
      Buffer = seq[Msg]
    
    var worst: Nanos
    
    proc mkMessage(n: int): Msg =
      result = newSeq[byte](1024)
      for i in 0 .. <result.len:
        result[i] = byte(n)
    
    proc pushMsg(b: var Buffer, highID: int) =
      let start = getTicks()
      
      let m = mkMessage(highID)
      # seqs copy, we don't want a copy here, so I used 'shallowCopy'.
      # Alternatively the 'ref array' version could be used.
      # Note that due to Nim's design we could also *re-use* the memory
      # completely, eliminating the GC runs but this would make this
      # benchmark pointless.
      shallowCopy(b[highID mod windowSize], m)
      
      GC_step(500) # 0.5ms, but GC never takes this long.
      
      let elapsed = getTicks() - start
      if elapsed > worst:
        worst = elapsed
    
    proc main() =
      GC_disable()
      # The MS backup collector is only required for cylic data structures,
      # but in this program we generate no cycles. Nim allows us to specify this
      # behaviour directly, so IMO it's fair to disable it. Nim's GC docs also
      # mention this fact, it's not undocumented voodoo:
      GC_disableMarkAndSweep()
      
      var b = newSeq[Msg](windowSize)
      for i in 0 .. <msgCount:
        pushMsg(b, i)
      
      # nano seconds into ms:
      echo("Worst push time: ", worst, "nano seconds")
      
      echo(GC_getStatistics())
    
    when isMainModule:
      main()
    

On my machine I get to roughly 23000 nano seconds.

Take with a big grain of salt!

  * Read my comments in the code.
  * I am not sure if "lib/system/timers" is reliable.
  * I have a fast CPU. :)


Reply via email to