https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

Tijl

09/07/2020, 10:42 AM
private 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 work
g

Glen

09/07/2020, 10:44 AM
OK, @Tijl, so how do I make the measuring function more accurate?
t

Tijl

09/07/2020, 10:45 AM
measure the actual work.
g

Glen

09/07/2020, 10:45 AM
Please, can you give me a hint?
t

Tijl

09/07/2020, 10:47 AM
it depends on which work you want to measure (I can’t tell you that, since that is up to you), but since you’re complaining about ASyncTask, and now you are measuring something that’s not comparable to anything in AsyncTask (only the launch of the coroutine) I doubt that is what you want measure
if you don’t understand what
launch
does, you can find a basic tutorial on coroutines, they pretty much all explain it.
g

Glen

09/07/2020, 10:51 AM
As for the scope, if you look at the function here , it delegates work to another Dispatcher for the background work, except I am doing it wrong.
👆 1
Any thoughts, @Tijl, or anyone?
w

wasyl

09/07/2020, 10:53 AM
The
withContext(Dispatchers.Default)
does move the heavy work to background thread so that should be correct. Also I’m not 100% sure but I think
measureTimeMillis
should work as well, since
launch
only returns once all the work inside is finished.
(I was wrong, see later comments) Can you elaborate on “results I obtain with AsyncTask are better”? Is AsyncTask version faster? If so, how much faster?
t

Tijl

09/07/2020, 10:53 AM
that doesn’t mean you are not telling it to go to the UI thread. From UI thread back to Default, back to UI thread is not free
b

bezrukov

09/07/2020, 10:54 AM
dispatcher's concurrency doesn't matter a lot here because all processing code is blocking and you don't use concurrency at all. I don't think you will see any "performance boost" just by switching from one async model to another without adoption/parallelization
g

Glen

09/07/2020, 11:00 AM
So, @bezrukov, how do you propose I tackle the issue?
@wasyl, for example, the execution time for coroutines is at least 4 times that of AsyncTask.
Not much difference in CPU usage also.
t

Tijl

09/07/2020, 11:02 AM
using your invalid measuring function?
g

Glen

09/07/2020, 11:02 AM
Yeah, @Tijl. Please could you provide an example of a more accurate measuring function?
t

Tijl

09/07/2020, 11:05 AM
I assume you don’t want to measure
launch
, 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.
g

Glen

09/07/2020, 11:07 AM
OK. I will try your suggestion.
b

bezrukov

09/07/2020, 11:08 AM
I wonder why it is not 0ms 🙂
g

Glen

09/07/2020, 11:09 AM
@bezrukov, you tested it?
b

bezrukov

09/07/2020, 11:11 AM
nope, just launch should end almost instantly. What is your numbers? I can't believe that launch will take at least 4ms (to be 4x time slower than async task) with the current measuring
g

Glen

09/07/2020, 11:13 AM
@bezrukov, with AsyncTask, I obtain 5ms. With coroutines, I obtain 15ms.
When testing on one person' device, coroutines gave me consistent 0ms and 1ms times, but on all other devices I have tested, the results are different.
By the way, @bezrukov, the AsyncTask version, is in the main branch of the repo.
b

bezrukov

09/07/2020, 11:58 AM
with asynctask you are measuring the time in a wrong way too, you're measuring
execute
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`:
Copy code
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.
t

Tijl

09/07/2020, 12:00 PM
throw in an
@jamesgoslin
while you’re at it 🤣
g

Glen

09/07/2020, 12:07 PM
OK. So @bezrukov, the parallelization should be done at a library level?
Please, anyone, could the error be associated with the version of the library I am using?
Hello everyone. Please can anyone give me hints on how to measure the execution time of those functions correctly?
5 Views