Skip to content

Commit

Permalink
make unzip faster: seq[i]=val can be 7X faster than seq.add(elem) (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour authored Feb 21, 2020
1 parent 1d90d98 commit e05aca8
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ proc unzip*[S, T](s: openArray[(S, T)]): (seq[S], seq[T]) {.since: (1, 1).} =
unzipped2 = @['a', 'b', 'c']
assert zipped.unzip() == (unzipped1, unzipped2)
assert zip(unzipped1, unzipped2).unzip() == (unzipped1, unzipped2)
result[0] = newSeqOfCap[S](s.len)
result[1] = newSeqOfCap[T](s.len)
for elem in s:
result[0].add(elem[0])
result[1].add(elem[1])
result[0] = newSeq[S](s.len)
result[1] = newSeq[T](s.len)
for i in 0..<s.len:
result[0][i] = s[i][0]
result[1][i] = s[i][1]

proc distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] =
## Splits and distributes a sequence `s` into `num` sub-sequences.
Expand Down

0 comments on commit e05aca8

Please sign in to comment.