Of course for plain compilers var parameters can be faster, because without 
smart optimizations result objects can be first allocated on the proc stack and 
then copied back to the caller variable. But I had the strong feeling that this 
optimization is generally done, so I never tried to avoiding result parameters. 
But my current observation is that a var parameter can be indeed faster, by 10 
to 20 % in my test case. (I discovered that fact by tuning my chess engine, so 
it is a real life observation...)
    
    
    import random, times
    
    type
      Data = tuple
        a: array[16, int]
        age: int
    
    proc p1(): Data =
      for i in mitems(result.a):
        i = random(7)
      result.age = random(7)
    
    proc p2(res: var Data) =
      for i in mitems(res.a):
        i = random(7)
      res.age = random(7)
    
    proc main =
      var h: Data
      var sum: int
      var t: float # cpuTime()
      t = cpuTime()
      for x in 0 .. 9999:
        h = p1()
        sum += h.age
      echo sum
      echo "p1: ", cpuTime() - t
      
      t = cpuTime()
      for x in 0 .. 9999:
        p2(h)
        sum += h.age
      echo sum
      echo "p2: ", cpuTime() - t
    
    main()
    
    # $ nim c -d:release  ups.nim
    # ~/nim-chess2 $ ./ups
    # 29915
    # p1: 0.000519
    # 60011
    # p2: 0.00042
    
    

Reply via email to