-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95b7880
commit 037f7fb
Showing
2 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/java/ru/gordinmitya/yuv2buf_demo/MovingAverage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ru.gordinmitya.yuv2buf_demo | ||
|
||
import kotlin.math.min | ||
|
||
class MovingAverage(val capacity: Int) { | ||
private var pointer: Int = 0 | ||
private val array: LongArray = LongArray(capacity) | ||
|
||
fun add(time: Long) { | ||
array[pointer % capacity] = time | ||
pointer += 1 | ||
} | ||
|
||
fun avg(): Double { | ||
if (pointer == 0) return 0.0 | ||
var sum = 0.0 | ||
for (l in 0 until size()) | ||
sum += array[l] | ||
return sum / size() | ||
} | ||
|
||
fun size(): Int { | ||
return min(pointer, capacity) | ||
} | ||
|
||
override fun toString(): String { | ||
return String.format("avg of %d = %.2fms", size(), avg()) | ||
} | ||
} |