The MicroProfiler is an optimization tool available in Roblox Studio that helps developers improve performance by visually representing unoptimized portions of their experience. This tutorial will showcase how to use the MicroProfiler to identify problematic segments of your experience and use it to find the cause of the poor performance.
For a more hands-on look at this process, you can download the tutorial project file here and follow along.
MicroProfiler isn't visible by default and is summoned by keystroke. You can summon it any time, but it's most useful when an experience is running.
Start by play testing your experience by pressing Play in the Home Menu.
Summon MicroProfiler by pressing Control+Alt+F6. You can see the render time for each individual frame represented at the top by the individual vertical orange bars. The higher the orange bar, the longer that particular frame takes to render.
MicroProfiler is constantly running, analyizing the render time for every frame. To see useful data, you need to pause MircoProfiler and analyze render information on a frame-by-frame basis. Press Ctrl+P to pause the MicroProfiler.
Each orange bar represents one frame, with the vertical height proportional to how long it takes that frame to render. You can tell something is wrong by how high the bars are. In the top right you'll see a green section that represents the current screen space. If you hover over the orange bars, you can select an individual frame for closer inspection.
On the left side of MicroProfiler, you'll see individual performance thread names. Click and drag down to find the largest horizontal category in that frame.
The horizontal length signifies the time duration this frame takes to render. A shorter horizontal length typically indicates well-optimized code. The vertical height is a hierarchy that signifies layers of code.
Identifying the Problem
Hovering your mouse over each section reveals information pertaining to that portion of code.
Hover over RunService.Stepped and notice it takes about 26 milliseconds to render.
Hover over LocalScript and notice that something is being called thousands of times.
Continue down the list and notice the Raycast label. This indicates that the problem might be related to raycasting.
Creating Troubleshoot Labels
Now that MicroProfiler has provided a starting point, it's time to troubleshoot and review the code.
Stop the play test and look at the LocalScript file to search for the problem.
This portion of code appears to be the culprit for the experience's poor performance:local function onStepped()debug.profilebegin("RaycastSpam")for _ = 1, RAYS_PER_SECOND dolocal startPosition = getRandomPosition()local endPosition = getRandomPosition()local direction = endPosition - startPositionWorkspace:Raycast(startPosition,endPosition)enddebug.profileend()end
Looking in our LocalScript in the RenderStepped event we see that there are rays being casted 1500 times per second. To make sure that this portion of code is indeed what's causing the rendering delay, give it a label that will highlight this portion of code when MicroProfiler is run.
Create a label using the debug.profilebegin() tag and call it Raycast Spam.
Close the tag by using debug.profileend().
Confirming the Problem
If the large horizontal bars in MicroProfiler are wrapped in this tag you will know that you were successful in identifying the unoptimized code.
Playtest your experience.
Pause MicroProfiler using Ctrl+P.
Click and drag down to find the largest horizontal category in that frame.
Look for the newly created Raycast Spam tag