Codebase list golang-github-vbauerster-mpb / 6ee0d6f
Reduce time.Now calls Vladimir Bauer 8 years ago
1 changed file(s) with 10 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
140140 }
141141 select {
142142 case b.ops <- func(s *state) {
143 next := time.Now()
143144 if s.current == 0 {
144 s.startTime = time.Now()
145 s.blockStartTime = s.startTime
145 s.startTime = next
146 s.blockStartTime = next
147 } else {
148 now := time.Now()
149 s.updateTimePerItemEstimate(n, now, next)
150 s.timeElapsed = now.Sub(s.startTime)
146151 }
147152 sum := s.current + int64(n)
148 s.timeElapsed = time.Since(s.startTime)
149 s.updateTimePerItemEstimate(n)
150153 if s.total > 0 && sum >= s.total {
151154 if s.dynamic {
152155 sum -= sum * s.dropRatio / 100
157160 return
158161 }
159162 s.current = sum
160 s.blockStartTime = time.Now()
161163 }:
162164 case <-b.quit:
163165 }
328330 return buf
329331 }
330332
331 func (s *state) updateTimePerItemEstimate(amount int) {
332 lastBlockTime := time.Since(s.blockStartTime) // shorthand for time.Now().Sub(t)
333 func (s *state) updateTimePerItemEstimate(amount int, now, next time.Time) {
334 lastBlockTime := now.Sub(s.blockStartTime)
333335 lastItemEstimate := float64(lastBlockTime) / float64(amount)
334336 s.timePerItem = time.Duration((s.etaAlpha * lastItemEstimate) + (1-s.etaAlpha)*float64(s.timePerItem))
337 s.blockStartTime = next
335338 }
336339
337340 func (s *state) isFull() bool {