{"id":4465,"date":"2014-03-30T11:17:44","date_gmt":"2014-03-30T11:17:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-program-optimisation-collection-of-common-programming-errors\/"},"modified":"2014-03-30T11:17:44","modified_gmt":"2014-03-30T11:17:44","slug":"c-program-optimisation-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-program-optimisation-collection-of-common-programming-errors\/","title":{"rendered":"C# Program Optimisation-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn Hi,I am developing a very resource-hungry application that runs more slowly than I would like. I have heard that C# performs many checks at runtime that use up many resources (I think it was Wikipedia) &#8211; is it possible to turn those off as surley I would only need them while debugging?<\/li>\n<li>\n<h3>8 Answers<\/h3>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn1 The C# compiler relies upon the JIT to do most of the optimization.\u00a0 When you &#8220;build&#8221; your program in Visual Studio, it does little more than convert the C# code to an MSIL program.The JIT, which turns the MSIL into machine-language code that your computer actually uses, is not optimized for long-running processes.\u00a0 It&#8217;s designed for fast compilation, rather than quality compilation.\u00a0 This is because the traditional .NET app is a simple UI application where the delays associated with compilation might cause an undesirable lag for the user.You can use the NGEN utility to pre-compile your application for the CPU.\u00a0 NGEN doesn&#8217;t face the same compile time constraints as the JIT assumes, so it can theoretically take all the time needed to produce a faster program.\u00a0 Unfortunately, as of today, NGEN doesn&#8217;t really do much optimization either (at least, not as much as it could).C++ compilers almost always produce faster programs because they&#8217;ve been tuned and enhanced for many years.-Ryan \/ Kardax<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn2 Why is C++ faster than C#?Surley all the processing should be done at compile-time.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn3 The C# compiler relies upon the JIT to do most of the optimization.\u00a0 When you &#8220;build&#8221; your program in Visual Studio, it does little more than convert the C# code to an MSIL program.The JIT, which turns the MSIL into machine-language code that your computer actually uses, is not optimized for long-running processes.\u00a0 It&#8217;s designed for fast compilation, rather than quality compilation.\u00a0 This is because the traditional .NET app is a simple UI application where the delays associated with compilation might cause an undesirable lag for the user.You can use the NGEN utility to pre-compile your application for the CPU.\u00a0 NGEN doesn&#8217;t face the same compile time constraints as the JIT assumes, so it can theoretically take all the time needed to produce a faster program.\u00a0 Unfortunately, as of today, NGEN doesn&#8217;t really do much optimization either (at least, not as much as it could).C++ compilers almost always produce faster programs because they&#8217;ve been tuned and enhanced for many years.-Ryan \/ Kardax<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn4<\/p>\n<p>Do you know of any good programs to convert C# to C++?<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn5 It&#8217;s not really possible to convert a whole C# project to C++ automatically.\u00a0 A major integral feature of C# is a garbage collector; C++ does not natively have this, so a conversion would be required to figure out all the object lifetimes.\u00a0 A human could do that (laboriously), but an automated system could not.<\/p>\n<p>With proper design, it&#8217;s possible to get most computationally-intensive C# applications to within 80-85% of C++ performance.\u00a0 Given how much work would be required to convert from C# to C++ for such a small gain, it&#8217;s probably easier to buy a faster computer <img decoding=\"async\" src=\"http:\/\/forums.microsoft.com\/MSDN\/emoticons\/emotion-1.gif\" \/><\/p>\n<p>-Ryan \/ Kardax<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn6 Would it be possible to turn off garbage collection?Also, why isn&#8217;t garbage collection done at compile-time? Surley that&#8217;s why we have to enter a static variable name.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn7 If garbage collection were disabled, there would be no way to release the memory used by objects.\u00a0 C# has no &#8220;delete&#8221; command.\u00a0 This feature is thus required, and always on.Garbage collection can&#8217;t be done only at compile time because new object instances can be created at run time (and always are in non-trival programs).For a well-designed program, the garbage collector has no effect on performance.\u00a0 A program that does not do any memory allocations during its heavy processing phases will not trigger any garbage collection activity.-Ryan \/ Kardax<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn8<\/p>\n<p>C# 3.0 lets you dump variables.<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>msdn Hi,I am developing a very resource-hungry application that runs more slowly than I would like. I have heard that C# performs many checks at runtime that use up many resources (I think it was Wikipedia) &#8211; is it possible to turn those off as surley I would only need them while debugging? 8 Answers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4465","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4465"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4465\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}