r/androiddev • u/lilfluf • 12d ago
Question How do I find slow spots in a single function?
So I have this one somewhat critical function that I want to be fast. I want to profile it with either method tracing, or callstack sample, to find what I can optimize in it. I've got two issues:
- If I just run profiling in Android Studio, I'm getting a humongous file (>300MB) as I'm trying to get the method to run in a realistic scenario. It's not run often, and to trigger it I must do things that run a lot of other code.
- This function is called from multiple places and multiple threads. Ideally, I would like to see the statistics for it, such as the flame chart, as a single entity. Instead, in the charts I see it in multiple places.
To overcome these issues, and primarily the first one, I would like to have profiling enabled as I enter the function and disabled as I exit it. Sounds simple? Well, I couldn't find a sensible way to do this. There are methods such as startMethodTracingSampling which work.. but they create a new file for invocation, while what I want is some aggregate statistics. And this file is on the device so I have spend some effor getting it from there.
Otherwise, I'm left with dealing with the humongous trace files. My computer can handle those, but again, very slowly, and finding my method of interest takes so much time that comparing the impact of code changes is just not viable.
Is there sensible way to quickly profile a single method? Or am I stuck with the above options?

