Glen
09/07/2020, 10:27 AMTijl
09/07/2020, 10:42 AMprivate val uiScope : CoroutineScope = CoroutineScope(Dispatchers.Main) <-- you’re processing on a single UI thread
your measure function is wrong though, you’re measuring the launch, only potentially the actual workGlen
09/07/2020, 10:44 AMTijl
09/07/2020, 10:45 AMGlen
09/07/2020, 10:45 AMTijl
09/07/2020, 10:47 AMTijl
09/07/2020, 10:48 AMlaunch does, you can find a basic tutorial on coroutines, they pretty much all explain it.Glen
09/07/2020, 10:51 AMGlen
09/07/2020, 10:53 AMwasyl
09/07/2020, 10:53 AMwithContext(Dispatchers.Default) does move the heavy work to background thread so that should be correct.
measureTimeMillis should work as well, since launch only returns once all the work inside is finished.Tijl
09/07/2020, 10:53 AMbezrukov
09/07/2020, 10:54 AMGlen
09/07/2020, 11:00 AMGlen
09/07/2020, 11:01 AMGlen
09/07/2020, 11:01 AMTijl
09/07/2020, 11:02 AMGlen
09/07/2020, 11:02 AMTijl
09/07/2020, 11:05 AMlaunch, I don’t know what you do want to measure. But for example a measureTimeMillis after withContext(Dispatchers.Default){ would correctly measure how long decoding an image would take. I don’t know why you would want to measure that, but it’s an example of correctly measuring something.Glen
09/07/2020, 11:07 AMbezrukov
09/07/2020, 11:08 AMGlen
09/07/2020, 11:09 AMbezrukov
09/07/2020, 11:11 AMGlen
09/07/2020, 11:13 AMGlen
09/07/2020, 11:15 AMGlen
09/07/2020, 11:25 AMbezrukov
09/07/2020, 11:58 AMexecute time. You need to measure doInBackground time instead (even better to measure the time between onPreExecute and onPostExecute).
As of coroutines you need to measure the time inside the `launch`:
uiScope.launch {
val time = measureTimeMillis {
val processedImage = processImage(imgView)
imgView.setImageBitmap(processedImage)
}
// print time
}
your current measuring doesn't provide any meaningful info about the consumed time.
But again, as I said in both asynctask/coroutines implementation your processing is blocking and singlethreaded and I don't think you will be able to improve this without digging into decoding process and parallelization it.Tijl
09/07/2020, 12:00 PM@jamesgoslin while you’re at it 🤣Glen
09/07/2020, 12:07 PMGlen
09/07/2020, 12:18 PMGlen
09/08/2020, 2:10 PM