{"id":196,"date":"2010-02-14T22:58:58","date_gmt":"2010-02-14T21:58:58","guid":{"rendered":"http:\/\/www.frozax.com\/blog\/?p=196"},"modified":"2010-02-14T22:58:58","modified_gmt":"2010-02-14T21:58:58","slug":"nprof-simple-efficient-free-xna-profiler","status":"publish","type":"post","link":"https:\/\/www.frozax.com\/blog\/2010\/02\/nprof-simple-efficient-free-xna-profiler\/","title":{"rendered":"NProf : A Simple, Efficient and Free XNA Profiler (3.1 and 4.0)"},"content":{"rendered":"<p><strong><span style=\"text-decoration: underline;\">Update<\/span><\/strong>: I have many visitors coming here from search engines. So here is a quick update if you want to use NProf with <strong>XNA 4.0<\/strong>. When you run NProf with your game, if the UI of NProf stays empty after running your game, it&#8217;s because you are running a XNA 4.0 game. There is a workaround <a href=\"http:\/\/communistgames.blogspot.com\/2010\/10\/using-nprof-011-and-clrprofiler-in-net.html\">here<\/a>: Run NProf.exe with a .bat containing:<\/p>\n<blockquote><p>set COMPLUS_ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler<br \/>\nNProf.exe<\/p><\/blockquote>\n<h3>Original post:<\/h3>\n<p>On its <a href=\"http:\/\/code.google.com\/p\/nprof\/\">google code page<\/a>, NProf is described as a <em>statistical profiler for .NET applications<\/em>. Good news: XNA being .NET, it can be used to profile games.<\/p>\n<p>It&#8217;s well known that in order to optimize, you first need to find what part of your code is slow. That&#8217;s where a profiler comes handy, it finds the bottlenecks of your game and then you can optimize what needs to be.<\/p>\n<p>The title of the post says it all, NProf is trivial to use, efficient and free. Once you download it, you select your XNA game executable, play it for some time and that&#8217;s it! I spent some time trying to find great profilers when I developed for PC and Mac but could not find anything really usable and affordable. I finally timed critical code sections with custom code and studied the log manually. Fortunately, NProf does all that for me in XNA.<\/p>\n<p>I had a few surprises when I ran NProf. For instance, yesterday I could see that <strong>21% of the game<\/strong> was spent in one method:<br \/>\nColor.Color( Vector3 )<\/p>\n<p>This XNA <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb195929.aspx\">constructor<\/a> converts a Vector3 in a Color and it is called a lot in my game to create my background effect (<a href=\"http:\/\/www.youtube.com\/watch?v=Pf4ELAm7oug\">see an old version in motion here<\/a>). In this constructor, the expensive call is PackUtils.ClampAndRound. I do not have access to this code, but I can use another basic constructor and do the conversion myself. Here is what I ended up doing:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><em><strong>Before<\/strong><\/em><\/td>\n<td style=\"text-align: left;\">\n<pre class=\"csharpcode\">Color col = <span class=\"kwrd\">new<\/span> Color( vColor +\n   \u00a0<span class=\"kwrd\">new<\/span> Vector3( start_val, start_val, start_val ) );<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><em><strong>After<\/strong><\/em><\/td>\n<td>\n<pre class=\"csharpcode\"><span class=\"kwrd\">int<\/span> r = (<span class=\"kwrd\">int<\/span>)(( vColor.X + start_val) * 255 );\n<span class=\"kwrd\">int<\/span> g = (<span class=\"kwrd\">int<\/span>)(( vColor.Y + start_val) * 255 );\n<span class=\"kwrd\">int<\/span> b = (<span class=\"kwrd\">int<\/span>)(( vColor.Z + start_val) * 255 );\nColor col = <span class=\"kwrd\">new<\/span> Color( ( r &gt; 255 ) ? (<span class=\"kwrd\">byte<\/span>)255 : (<span class=\"kwrd\">byte<\/span>)r,\n    ( g &gt; 255 ) ? (<span class=\"kwrd\">byte<\/span>)255 : (<span class=\"kwrd\">byte<\/span>)g,\n    ( b &gt; 255 ) ? (<span class=\"kwrd\">byte<\/span>)255 : (<span class=\"kwrd\">byte<\/span>)b );<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The new code doesn&#8217;t clamp values below zero but I don&#8217;t need that feature. The really important thing is that it is now more than <strong>12 times faster <\/strong>than before!<\/p>\n<p>Without NProf, I would not have guessed that this constructor was that critical.<\/p>\n<p>The main problem I see with NProf is that it only profiles your game on Windows and bottlenecks may be different on the X360.<\/p>\n<p>Anyway, I really advise you to run your game in this profiler before you optimise anything. Be sure to use a release build.<\/p>\n<p>Feel free to share your experiences with NProf!<\/p>\n<div class=\"addtoany_share_save_container addtoany_content_bottom\"><div class=\"a2a_kit a2a_kit_size_32 addtoany_list a2a_target\" id=\"wpa2a_1\"><a class=\"a2a_button_twitter\" href=\"http:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F02%2Fnprof-simple-efficient-free-xna-profiler%2F&amp;linkname=NProf%20%3A%20A%20Simple%2C%20Efficient%20and%20Free%20XNA%20Profiler%20%283.1%20and%204.0%29\" title=\"Twitter\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_facebook\" href=\"http:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F02%2Fnprof-simple-efficient-free-xna-profiler%2F&amp;linkname=NProf%20%3A%20A%20Simple%2C%20Efficient%20and%20Free%20XNA%20Profiler%20%283.1%20and%204.0%29\" title=\"Facebook\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_google_plus\" href=\"http:\/\/www.addtoany.com\/add_to\/google_plus?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F02%2Fnprof-simple-efficient-free-xna-profiler%2F&amp;linkname=NProf%20%3A%20A%20Simple%2C%20Efficient%20and%20Free%20XNA%20Profiler%20%283.1%20and%204.0%29\" title=\"Google+\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"http:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F02%2Fnprof-simple-efficient-free-xna-profiler%2F&amp;linkname=NProf%20%3A%20A%20Simple%2C%20Efficient%20and%20Free%20XNA%20Profiler%20%283.1%20and%204.0%29\" title=\"Reddit\" rel=\"nofollow\" target=\"_blank\"><\/a>\n<script type=\"text\/javascript\"><!--\nwpa2a.script_load();\n\/\/--><\/script>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Update: I have many visitors coming here from search engines. So here is a quick update if you want to use NProf with XNA 4.0. When you run NProf with your game, if the UI of NProf stays empty after running your game, it&#8217;s because you are running a XNA 4.0 game. There is a&hellip; <a class=\"more-link\" href=\"https:\/\/www.frozax.com\/blog\/2010\/02\/nprof-simple-efficient-free-xna-profiler\/\">Continue reading <span class=\"screen-reader-text\">NProf : A Simple, Efficient and Free XNA Profiler (3.1 and 4.0)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,12,21],"tags":[99,113,114],"_links":{"self":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/196"}],"collection":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":0,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}