Skip to content

Instantly share code, notes, and snippets.

@glureau-betclic
Last active August 7, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glureau-betclic/10405fec98b465a89e024bf53b717b26 to your computer and use it in GitHub Desktop.
Save glureau-betclic/10405fec98b465a89e024bf53b717b26 to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
private var switchFromIoToCompute = 0L
private var switchFromComputeToIo = 0L
private var loopCount = 0L
private var appCreated = 0L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val publishSubject = PublishSubject.create<Long>()
var startTime: Long = 0L
publishSubject.doOnNext {
startTime = System.nanoTime()
}
.observeOn(Schedulers.computation())
.doOnNext {
switchFromIoToCompute += System.nanoTime() - startTime
startTime = System.nanoTime()
}
.observeOn(Schedulers.io())
.doOnNext {
switchFromComputeToIo += System.nanoTime() - startTime
}
.subscribe {
// Loop
loopCount++
publishSubject.onNext(it + 1)
}
appCreated = System.nanoTime()
publishSubject.onNext(0L)
Observable.interval(0, 1, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.subscribe { updateUi() }
}
private fun updateUi() {
val totalSwitching = (switchFromIoToCompute + switchFromComputeToIo) / loopCount
val appTimeByLoop = (System.nanoTime() - appCreated) / loopCount
val message = """
loopCount = $loopCount
IO -> Compute = ${switchFromIoToCompute / loopCount}
Compute -> IO = ${switchFromComputeToIo / loopCount}
Total switching = $totalSwitching
Total time since activity created / loop count= $appTimeByLoop
switch time / Percent total time = ${(totalSwitching * 100f) / appTimeByLoop}%
""".trimIndent()
text_view.text = message
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment