A platform profiler measures where an application spends CPU time, memory, or other resources in its real runtime environment. CPU profilers commonly produce call trees or flame graphs: start with the widest or most expensive path, then expand through framework layers until reaching application code that can be changed. Sampling gives a lower-overhead picture of normal behavior; tracing records every call for exact sequences but can distort timing. section 7.2.3 section 7.3.1
Profile a representative user flow rather than an isolated function. On a physical lower-end device, launch the app, navigate screens, scroll lists, trigger animations, and perform frequent operations long enough to capture typical use. Treat expensive work differently according to context: CPU use during a user-requested calculation may be expected, while activity during apparent idle time can expose unnecessary background work. section 7.3.1 section 7.3.6
Use profiler results as an investigation workflow:
- Rank functions by time, follow the heaviest call chain, and distinguish code that owns the cost from code that merely calls it. section 7.2.3
- Prefer sampling as the first pass; use trace-based recording when a specific sequence needs exact detail despite added overhead. section 7.3.1
- Compare measurements with a performance budget, such as time to first interactive screen, a 16 ms frame deadline, or a memory ceiling. section 7.3.6 section 7.4.1
- Re-profile after a change and verify that the target metric improved without leaving continuing idle work or resource growth behind. section 7.4.1 section 7.4.2
Reading cumulative versus self time
A Python profile can show serialize_response with 400 ms cumulative time but only 5 ms self time. The function is not the main cost; follow its callees to locate the remaining 395 ms. By contrast, a function with high self time is a direct candidate for investigation. cProfile can collect function-level timing, sort results by cumulative time, and print the most expensive entries. section 7.2.3
Listing 7.3 Python CPU profiling with cProfile
import cProfile
import pstats
profiler = cProfile.Profile() #A
profiler.enable()
# Your code here
process_request(request_data) #B
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative') #C
stats.print_stats(20) #DThis cProfile setup brackets a request, sorts collected statistics by cumulative time, and prints the leading functions for call-chain investigation. section 7.2.3
Profiler choice is platform-specific, but the investigative pattern is shared. Instruments and Android Studio expose mobile CPU work; Visual Studio provides CPU call trees and flame charts; deeper Windows tracing can add system scheduling, disk, memory, and GPU context after an application-level profiler narrows the problem. Memory profilers complement CPU analysis by comparing snapshots across long-running sessions to reveal continued growth rather than expected startup allocation. section 7.4.1 section 7.4.2
Make profiling routine before review and before release, not a post-regression emergency. A lightweight check can run the relevant platform tool and compare one agreed metric with its budget; teams can define which changes require it and track completion. This catches costly behavior while the code is fresh, before slow exports, lag, or frame drops reach users. section 7.6 section 2.1.3




