Unity 101 - Using the Unity Profiler to optimize performance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up Jason here from unity3d college today I want to talk about profiling I'm gonna start off with a basic example where I've set up some things to be intentionally slow and we'll run through some of the really simple stuff and then I'm gonna dive into a semi real project and we'll do some real kind of live profiling and I'll go through and see if I can find issues and see if we can maybe fix some of them so to start off I said I've got this simple little demo scene here I've just got a bunch of the same cube laid out and the cube has a single script on it just called slow code so what I'm gonna do is just run we're gonna look at the profiler window and see what we can figure out and then I'll show you kind of what what everything means and what to look for so here we go we'll hit play and if you don't know where the profiler window is by the way just go to window and then profiler right here there's also this little stats window somewhat useful but the profiler is you know drastically better has a whole lot more info so let's watch and see so writing T these blocks are just kind of flashing relatively randomly it's actually cycling through just a bunch of colors and if we look at the profiler here and hit current what you're gonna see is the the most recent second basically of profiling data so you can see the is it updates I think about once a second so you can see right here we've got update script run behavior is at the top by default it's filtered or sorted by the time in milliseconds and you can see that this call right here is taking up about 30 milliseconds and if you look at the bar here you can see that that we've got a frame rate lying right here so if we're under or right at that line where it says 60 it means that we're running at 60 frames a second here we're spiking to as low as 30 frames a second in fact as we watch it's actually going above that so we're dipping below 30 frames a second sometimes now what I can do is just click anywhere on here and we're it'll pause the game and give me the info for that specific frame and I can kind of scroll through here and look at them I can hit current to go to the most recent frame or you can use these arrows to go back and forth a single frame so if we're looking for like maybe want to find this exact spike and I clicked over here I can always just click the arrow to get over to it or I could drag and find it sometimes it's easier to use the arrows though so let's take a look at just one random spot here so I've just got this one picked so let's go to this one it's a little bit bigger and then what we can do is expand out this script run behavior update and you'll see here under behavior update we've got a slow code update that's the code that we're using on these cubes and I want to point out a couple things first is the percentage of total that's just how much of the the profiling or the CPU time was used by this as opposed to everything else in the profiler so we've got ninety six point eight percent ninety seven percent it's all in here which makes sense because this is really the only thing going on in here but then we also have calls so 25 is the number of times this thing is called and again that kind of lines up with our 25 cubes you have five rows of five cubes so this is getting called once for every object on there the next thing we have is the garbage collector allocation this is the amount of memory that's being allocated that's going to need to be cleaned up in a garbage collection you usually want this to be zero sometimes there are some games where you can have a little bit of garbage collection but ideally you want to get it down to as low as possible or zero and then here we the final thing I want to look at really is the time in milliseconds so this is just how long we spent doing this action in this frame now if you look underneath you'll see we only have one thing and it's just this log string to console and that's a pretty obvious one this is something that you're gonna see a lot if you start profiling logging is terribly slow but I want to see the other things too there's actually other stuff here so what I need to do is enable the deep profile option right here it's kind of hard to notice just little box and then I have to hit reload most games you're gonna have to hit that and then restart since this is really simple and there's no state at all I think it'll be fine to just continue on out whatever let's just do a restart anyway just a little bit more realistic most games just don't work very well with an editing continue style mode Oh what happened there okay let's stop let's go to and hit play one more time okay so now we're getting something really slow notice how our frame rate is terribly terribly slow that's because I turned on deep profile and we have a really bad loop in here so the first thing before we dive in and start fixing these things I want to show the what's in the profiler so what we're seeing here and then we'll jump to the actual functions so you see here the first thing in here is the do loop stuff and the reason that this is dying is that when we do deep profiling we're getting a lot more info and this loop is actually looping over something a thousand times so if you look here we have 25 calls and it's calling material but setcolor 25,000 times again this is more of a sample just to really show the numbers and show the big spikes so it's really obvious what we're doing and we'll jump into a real project shortly after so let's look at the code so if we just open up the slow code and go to the top you see the first thing we do is just update the renderers material to a color for time value and this is just um pretty unimportant we're just picking a random color so if we go down to here you see are not a random color so we're cycling through colors so we just increment some values if they go greater than one we said we subtract one from them and I just do them a different value so that way they kind of semi cycle through different colors but this isn't a problem right so if we look at our code again let's look at color for time um okay I was wrong this is a problem when you call it 25,000 times so we want to not do that so the first step is obviously just get rid of this stupid loop because we're not actually doing anything useful there and again not a super helpful thing to get rid of but cuz you're not gonna have that in a real-world situation I hope but it's uh just getting to show what's going on so now we run it with the profile on and we'll see we're actually getting reasonable performance it's not great performance but we're getting you know we're not getting half a frame a second or we were out before thank you at the stats window it looks like we're at 10 frames a second so let's take another look and see what we can find so just pick a random spot on here and let's see what else you've got it so if we expand out we've got slow code update and the next thing is and do something else expand that out and of course this is that debug log call and a debug logs are extremely slow because they get the call stack and I've seen it can kill performance many many times and games from people accidentally leaving them and able and just forgetting to turn them off or delete them out so this is one of the reasons I try to not use debug log too much just use it temporarily then get rid of it right after I'm done with it so let's delete that out let's see that was in oh this is that's for our next project let's see you let's open up slow code one more time and if we got to do something else and see it's actually doing just that so just delete it stop hit play one more time and everything should be relatively fast now right so now we're getting good framerate because we found the couple things that were intentionally made bad now in your project you're gonna have things that are accidentally slow I do all the time so just kind of expect that hopefully you don't have loops of a thousand things doing the exact same thing now let's jump over to the other project because I think that's gonna be a little bit more interesting hopefully you've got a decent idea of how the profiler works and what to look at oh I've got a lot of error messages here so this is just a little spaceship game and it's been playing around with on the side for fun mostly just to play with controls and come up with other different ideas and it's also a great source for demo material so I got my controller here and I'm gonna hit play and if I just hit Y it adds in some bots so you'll see the the menus right here start going through randomly selecting things it's supposed to kind of look like they're players but it looks pretty bad I think so it just goes in and picks some some ships and then spawns and we already saw kind of a big spike though let's open up that profiler window and now we're gonna see some actual real data so let's take a look well let's let it go for just a minute wait until we see the next big spike maybe Oh things are dying the cameras not quite in position right now it's supposed to hover over them I think it's kind of broken you can see we've got a couple errors here well let's let's just stop and take a look now so what was this big spike we got this one big big spike that dropped us down to Oh some really slow yeah there was a three-second spike so almost three seconds 2.7 seconds I guess so what's causing that let's expand some things out take a look so under behavior update we have ship component update and we have ship component activate and ship weapon active tick and then fire weapon and just keep expanding it and we going to the pulled monobehaviour so this is my pooling system and expand it again and we get get a pool I'm just gonna keep going down and I bet it's right here in this grow pool so the way the pooling system works it just instantiates all of the objects that are needed for the pool but I think I have it in this project set up to do it kinda lazily so it waits until it actually needs the prefab to to spawn it instead of doing it in advance so one easy fix for this spike right here would be to just preload and pre-warm all of my pools for the different weapons and I think that's probably a little bit longer than this video will go on for it's gonna take a little while because we dynamically selecting a bunch of ships and then we have to figure out what all the weapons are on those ships but it's definitely a fix that I'd want to put in before releasing this anywhere so you want to pre warm those pools just by spotting the objects in advance let's see what other kind of issues we can find so if we go through here I'm gonna guess that these are all very similar to these other spikes ship selection okay what's this one no no that's not it it's in player Update canvases so here we're getting a slowdown from the UI so and it looks like it's rebuilding the layout of the UI and it stopped as soon as we think this is right when we started playing so I think this is something in the menu where that transition we're not actually switching scenes we're actually just turning off the children here so it looks like something with these is causing a problem once they get disabled it looks like it kind of goes away so again not something I want to fix right now but now I know that that's an issue and I know that I should look at this when I'm ready to actually start optimizing the thing and I want to keep looking over here and see what other kinds of things I can find actually let me hit hit current and I'm gonna grab the scene view just drag it out here since my camera view is a little bit messed up right now and I'm just gonna resume and watch it go and let's wait and see what happens when somebody dies I'm positive there's gonna be a little spike there let's see there we go we got it somebody died and oh yeah look at that so everything started slowing down a little bit we're gonna speed up oh that's sick let's take a deeper look so here what do we have on our most recent spike let's see let's go to this one this is the most recent spike we've got so this one is under physics fixedupdate so what's happening here is whatever's causing this spike is originating from a collision or a trigger enter so let's see it looks like we have yep it's ontriggerenter of ship health and here it's dealing damage the ship is taking damage we're calling in to an event here and to go into on health changed and let's see you just keep going down this hip handle ship death and spawn looks like spawn next ship is kind of the root of our problem here so we're it's actually the instantiation of the next ship so if we look at spawn ship let's just open up the code let's see let's pull that up I think I got it right here let's find spawn ship so spawn ship actually just instantiates the next ship to spawn so an easy fix for this would be to pre instantiate all of the ships for the player the way it works now is if your ship dies we spawn the next ship and then you can fly around in that ship and then when that one dies to spawn the next one so Fix here would again just be to pre instantiate all of the players ships and then just disable them kind of push them off to the side and then re-enable them once that once we're ready instead of instead of spawning them right here so I think I'll skip doing that today just because I think it'll make this video a little bit longer than I want it before I draw it out in another 20 minutes or so and I think hopefully you've kind of got the idea of how to use the profiler what kind of things to look for remember logs are always a big hit and they see him every single time but hopefully again this helps if it does please make sure you share it with friends hit the like button and hit subscribe and thanks for watching [Music]
Info
Channel: Jason Weimann
Views: 66,851
Rating: undefined out of 5
Keywords: Unity, Unity3D, Unity Profiler, Unity3D Profiler, Game Profiler, Unity Performance, Unity Optimize, Unity Optimization, Unity FPS
Id: fROTtgZK-Zs
Channel Id: undefined
Length: 13min 57sec (837 seconds)
Published: Mon Nov 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.