S&box Wiki
Home
/
Edit CPU Performance Profiling
View
Edit
History
No Category
Developer Overview
The Project System
Publishing To Asset Party
Getting Started With Hammer
Mapping Basics
Mapping Entities
Advanced Mapping Techniques
Getting Started with Modeldoc
Animgraph & Animation
Physics
Modeldoc Nodes
Advanced Modelling
UI Basics
Styles & Stylesheets
Razor Templates
Game Menus
Materials
Built In Shaders
Shaders
Shader Reference
Sounds & Audio
Particles
Getting Started
Making Games
Input
Networking
Physics
Rendering
Editor & Tools
VR
Misc
Playing Guides
Console Commands & Variables
Dedicated Server
Log in to edit
CPU Performance Profiling
<cat>Code.Misc</cat> <title>CPU Performance Profiling</title> # CPU Performance Profiling Code profiling tools allow you to analyse the execution of your code during runtime and find any performance bottlenecks. This tutorial will use the profiling tools in Microsoft Visual Studio, but there is lots of other software that does the same thing. ## Instructions 1. Open up Microsoft Visual Studio. 2. Open up your gamemode in the game client or editor. The advantage of using the game client is that you avoid the overhead of the editor, which can make the execution analysis more accurate. Whether that matters much in practice though is debatable. 3. Go to `Debug` -> `Attach to Process...`. <upload src="b0cef/8db4dbbedc4c395.png" size="11536" name="image.png" /> 4. Find sbox.exe or sbox-dev.exe in the process list and press `Attach`. <upload src="b0cef/8db4dbc14c9b59f.png" size="53063" name="image.png" /> 5. In this menu that should pop up go to `CPU Usage`. You'll also want to press `Enable CPU profiling to see a breakdown of CPU usage by function`. <upload src="b0cef/8db4dbc6f9f07ae.png" size="9145" name="image.png" /> 6. Go back to your gamemode and do random stuff for about a minute or so. The longer you profile it for, the more accurate your results will be (but anything over 60 seconds is probably overkill). If there's something in particular you want to benchmark, then just spam that over and over again in-game. 7. When you are done, break all. <upload src="b0cef/8db4dbcd49ac3d0.png" size="2755" name="image.png" /> 8. It will do some stuff, be patient. Your game will be frozen now. 9. At this stage it can be a good idea to enable `Show Native Code` and disable `Show Just My Code`. By default the execution analyser will only show you what's going on in your code in the C# side of the engine. This is because what goes on in the C++ part of the engine is somewhat a secret since Facepunch do not make the debugging information for this public. Naturally that's a big chunk of code that would otherwise be missed. Setting these options will show the analysis of the C++ code; it's mostly gibberish, since we don't get actual function names, but sometimes it can still produce helpful clues. For example, in the screenshot below we can see that the animation system is using up a whopping 13% of the CPU time. Having said all that, for the rest of the tutorial, we will keep these at their defaults though just to keep things simple. <upload src="b0cef/8db4f3a0449f264.png" size="20125" name="image.png" /> <upload src="b0cef/8db4f3b2b95a14b.png" size="30600" name="image.png" /> 10. Click on any of the functions that show up here, e.g. `Sandbox.Program.Main()`. <upload src="b0cef/8db4dbcfbd8e1b8.png" size="7377" name="image.png" /> 11. Switch to `Call Tree` since it is probably the nicest way to view what's going on. <upload src="b0cef/8db4dbd08c16848.png" size="10734" name="image.png" /> 12. Find and expand `Sandbox.Program.Main()`. <upload src="b0cef/8db4dbd3221319b.png" size="50496" name="image.png" /> 13. This will basically show you the slowest functions that were called and how much total CPU time they took up. Main() is the starting point for your program, so that will always be high. However, we can see that NPCCar.Tick() is taking up 2.14% of our CPU time, which is quite a lot. We can navigate deeper into the tree to find out the cause. <upload src="b0cef/8db4dbd8aaa09d5.png" size="51657" name="image.png" /> 14. In this case the culprit was the function CarWheel.Raycast() making too many raycasts. Every car wheel is making a raycast every tick, and we have have 21 cars in the map, each with 4 wheels, totalling 5040 raycasts per second! Oops! 15. Press `Continue` or `Stop Debugging` to unfreeze the game. <upload src="b0cef/8db4dbdf803985f.png" size="5952" name="image.png" />
S&box Wiki
Development
Developer Overview
6
Editor Overview
General FAQ
System Requirements
The s&box wiki
Troubleshooting
Useful Links
The Project System
4
Adding Assets
Creating a Game Project
Project Settings Window - Games
Project Types
Publishing To Asset Party
2
Uploading assets
Uploading projects
Hammer
Getting Started With Hammer
3
Getting Started With Hammer
Making Your First Map
Mapping Resources
Mapping Basics
7
Cordons
Hotspot Materials
Selection Sets
Standard Mapping Dimensions
Tool Materials
Tools Visualisation Modes
Using Entities That Require a Mesh
Mapping Entities
2
Creating a Door
Light Entities
Advanced Mapping Techniques
8
Collaborating With Prefabs and Git
Instances
Prefabs
Quixel Bridge Plugin
Tilesets
Tilesets-Advanced
Tilesets-Proxies
VIS Optimizations
Models & Animation
Getting Started with Modeldoc
7
Automatic Model Setup
Breakpieces
Creating a Model
Guide to Models
Importing Rust Weapons
LODs
ModelDoc FAQ & best practices
Animgraph & Animation
4
Animations without Animgraph
AnimEvents, AnimGraph Tags, Attachments
Animgraph
Delta Animations
Physics
3
Cloth Physics
Collisions, Physics & Surface Types
Jiggle Bones
Modeldoc Nodes
1
Custom ModelDoc nodes
Advanced Modelling
6
Bodygroups
Citizen
First Person
IKChains and Stride Retargeting
Morphs
Vertex Normals
User Interface
UI Basics
7
Custom Fonts
Embedding Websites
Enabling Pointer Events
Events and Input
Localization
UI Basics
UI with Components
Styles & Stylesheets
1
Video Backgrounds
Razor Templates
4
A Razor Overview
Aliases and SetProperty Attributes
Generic Components
Templates
Game Menus
1
Making a Custom Pause Screen
Materials & Shaders
Materials
5
Guide to Materials
Material Attributes
Material Resources
Texture Settings
Using Dynamic Expressions
Built In Shaders
2
Foliage Shader
Glass Shader
Shaders
4
Compute Shaders
Constant Buffers
Material API
Shading Model
Shader Reference
5
Anatomy of Shader Files
Getting rid of Tex2D macros
Shader Reference
Shader States
Texture Format Cheat-Sheet
Other Assets
Sounds & Audio
3
Guide to Sounds
Sound Events
Soundscapes
Particles
5
Creating animated sprites
Creating your first particle effect
Understanding Particle Editor
Using custom sprites
Using particle systems from C#
Coding
Getting Started
5
Cheat Sheet
Learning Resources
Setting up Rider
Setting up Visual Studio
Setting up Visual Studio Code
Making Games
2
Components
GameObjects
Input
4
Commands
ConVars
Input System
Speech Recognition
Networking
7
Auth Tokens
Http Requests
Lobby System
Networked Types
Networking Basics
RPCs
WebSockets
Physics
5
Collisions
Hitboxes
Joints
Traces
Triggers
Rendering
3
Render Tags
RenderHooks
Scenes
Editor & Tools
7
Creating a Tool
Custom Asset Types
Guide to Widgets
Hammer API
Hammer Gizmos
Hotload Performance
Widget Docking
VR
3
Getting Started
VR Input
VR Overlays
Misc
13
Asset Types
Attributes and Component Properties
Backend API
Cloud Assets in code
Code Accesslist
CPU Performance Profiling
DisplayInfo
FileSystem
Mounting assets at runtime
package/find
Setting Up A Navigation Mesh
Threaded Tasks
TypeLibrary
Playing
Playing Guides
3
Default Keybinds
Proton
s&box on macOS (Experimental)
Console Commands & Variables
1
Launch Arguments
Dedicated Server
1
Dedicated Servers