开发者

.NET best coding practice to achieve compiler best performance?

Most of the time during coding, i found that whether i can write code in less number of line.

I don't know when we write some login in less number of line in c# then whether we achive good performance or not? whether dotnet compile开发者_JAVA技巧r compile code faster or not?

Is there any source of tutorial/ book/ guideline so that we will make checklist before writing code.


What we should consider is writing better codes that other people can understand, declaring better variable names, maintain structure and design.

Of course we can write a login in 3 lines of code. Does that means compiler can compile/run it faster? Well, we should keep in mind that now-a-days compilers are extremely smart. Writing small number of lines of code doesn't prove that we're smart. And it also doesn't make sense compiler will compile faster.

Compilers optimizes our code to bytecodes very well and hence we should consider writing code considering designs and principles and of course human can understand the code as self code documented.

Here I quote what Martin Fowler said:

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Hope this helps!

With Regards,
Munim


No, writing code in less lines in a high level language like C# will almost never have any significnant impact on performance. Compilers are designed to optimise these things anyway.

The key to coding for best performance is make the code maintainable and natural first. More often than not this will be optimised by your compiler into well performing code, because the compiler has been designed to recognize these normal structures and optimise them the best way it can.

Avoid heavily nested loops where possible. Don't do work multiple times in a loop when it can be done once before the loop starts. Avoid making multiple database or network calls when 1 will do the job.

Performance test your code when it is finished and look for a bottleneck. Then spend time reviewing this bottleneck to see if you can optimise it. This will give you the best performance improvement for your time. Don't waste time early on in the project trying to optimise little things until you know they have a big effect on performance.

Your biggest performance hits are likely to be file access, database access, web service access. Operational stuff you just do in code is not likely to have a huge performance hit.


To be honest, I think most people would prefer readable and maintainable code over code that compiles half a second faster.

If you're getting into the situation where your solution is taking minutes to compile, then you're clearly dealing with a lot of code, and the readability of it becomes all the more important.

Edit to add

Following on from some comments to the OP, work might well buy you faster CPUs and SSDs if you follow XKCD's advice:

.NET best coding practice to achieve compiler best performance?


"less number of line"? This is not so relevant. Computing n'th Fibonacci number can be implemented recursively with exponential complexity or by using matrix multiplication in logarithmic time, with more lines of code. Kolmogorov complexity is not always to be minimized.

Look a rule: avoid unnecessary boxing, as in:

int x=3; Console.WriteLine("x ={0}", x); //wrong; but: Console.WriteLine("x ={0}", x.ToString());

I always regarded the book Effective C#: 50 Specific Ways to Improve Your C# as a good book showing you how to write better code in C#; for example, it explains why you should use foreach instead of for when iterating over a collection.


If you are asking whether less lines of code is faster to compile than compiling more lines of code, the answer is, it depends.

You could potentially have 300 lines that compile far quicker than 50 lines.


You asked for a book or article. One of the best books for best practices in .NET is

  • Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET

The book is written by members of the .NET development team themselves.


please see this book reference "Improving .NET Application Performance and Scalability": http://msdn.microsoft.com/en-us/library/ff649152.aspx

This is very good tutorial which covers most part of .NET application


While not ASP.NET specific, I can highly recommend this free C#/VB.NET coding guidelines reference by Steven Sartain at SubMain. I don't think it will cover anything about how to write code that will compile fast, but I am not entirely sure why that would be anywhere near the top of you list of concerns. Just worry about writing good code.

http://submain.com/products/guidelines.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜