Why is using a sequence so much slower than using a list in this example

前端 未结 2 1515
暖寄归人
暖寄归人 2020-12-05 18:55

Background: I have a sequence of contiguous, time-stamped data. The data-sequence has holes in it, some large, others just a single missing value.
Whenever the hole is j

2条回答
  •  [愿得一人]
    2020-12-05 19:19

    Seq.skip constructs a new sequence. I think that is why your original approach is slow.

    My first inclination is to use a sequence expression and Seq.pairwise. This is fast and easy to read.

    let insertDummyValuesWhereASingleValueIsMissingSeq (timeBetweenContiguousValues : TimeSpan) (values : seq<(DateTime * float)>) =
      let sizeOfHolesToPatch = timeBetweenContiguousValues.Add timeBetweenContiguousValues // Only insert dummy-values when the gap is twice the normal
      seq {
        yield Seq.hd values
        for ((prevTime, _), ((currentTime, _) as next)) in Seq.pairwise values do
          let timeDiffBetweenPrevAndCurrentValue = currentTime.Subtract(prevTime)
          if timeDiffBetweenPrevAndCurrentValue = sizeOfHolesToPatch then
            let dummyValue = (prevTime.Add timeBetweenContiguousValues, 42.0) // 42 is chosen here for obvious reasons, making this comment superfluous
            yield dummyValue
          yield next
      }
    

提交回复
热议问题