I was wondering if slices are still copied when used as proc parameters...

Found again Araqs comment

[https://forum.nim-lang.org/t/4582#28715](https://forum.nim-lang.org/t/4582#28715)

So my guess was that they are still copied. But maybe overhead is small? So did 
this test:
    
    
    # https://github.com/nim-lang/Nim/blob/devel/lib/system/iterators.nim#L1
    
    # https://forum.nim-lang.org/t/4582#28715
    
    iterator span*[T](a: openArray[T]; j, k: Natural): T {.inline.} =
      assert k < a.len
      var i: int = j
      while i <= k:
        yield a[i]
        inc(i)
    
    type
      O = object
        i: int
    
    proc main =
      var
        s = newSeq[O](1000000)
      for i in 0 .. (1000000 - 1):
        s[i] = O(i: i)
      
      var sum = 0
      for x in s[1 .. ^1]:
      #for x in s.span(1, s.high):
        sum += x.i
      
      echo sum
    
    main()
    
    
    
    Run

Using span iterator reduces total running time by a factor of two. (And I think 
use cases are not that rare, I have a few where iteration has to skip first or 
last element.) Question: Will copying of Slices vanish in the future, or are 
span iterators useful. (At least they are less ugly than a while loop.)

Reply via email to