4

Possible Duplicate:
Effect of unused methods and properties on library or executable

I am working on a project in which I am using existing application for development, and doing customization as per the project requirement. While customizing I found a lot of functions & methods that are of no use in current project. I am thinking of keeping that code as it is and doing customization in the required code only, but I am not very sure about how much this unreachable code will affect the performance of my application. Should I keep them as-is or remove them?

EDIT: In my application DataInteraction assembly contain 20 methods out of which 2 are in use and rest of them not, but as per my knowledge if any of the method of a assembly is called than entire assembly get loaded into memory and if yes than I feel it will effect performance.

Community
  • 1
  • 1
funsukvangdu
  • 1,621
  • 4
  • 20
  • 33
  • 6
    A better question would be how much does premature optimization affect the performance of developers? If you don't have a performance problem, then don't worry about the performance. You no doubt have many other problems to concern yourself with. – John Saunders Jan 22 '13 at 03:21
  • Hi John. I have given task to improve performance of this application by my PM thats why I am thinking from this point. – funsukvangdu Jan 22 '13 at 03:26
  • 2
    @AshishKhandelwal if your task to improve performance, you should start by discovering the performance problems. Do profiling first. As for your edit, two methods are hardly worth an assembly, try moving them to another or ilmerge them. – Theraot Jan 22 '13 at 03:31
  • 2
    @AshishKhandelwal: your technique is precisely what I'm referring to. The way to solve a performance problem is, first, to _discover_ a problem, then to get details of where the problem is, then to fix the problem, measure again, fix more, measure again, etc. The absolute _worse_ way to fix performance problems starts with guessing where such a problem might exist, then proceeding to **fix the wrong problem**. – John Saunders Jan 22 '13 at 03:33
  • @@All: Still I am confused. If all methods are of same assembly and called inside same using statement than will it effect performance or not if some methods are in use and some not – funsukvangdu Jan 22 '13 at 03:48
  • @@Hans : This questions is taking about using statement for libraries that don't get used. My question is using statement for libraries that are parially used – funsukvangdu Jan 22 '13 at 04:29
  • dkackman have the answer - yes, but for different reasons - unused code distracts developers, hence make the rest of the code slower as less time spent measuring and improving actively used code. – Alexei Levenkov Jan 22 '13 at 05:31

4 Answers4

4

It just bloats the executable size.. if the methods aren't called, they aren't JIT'd..

..you should definitely think about cleaning them up though. What is their use? They bloat the code and make using the code base worse..

I also agree with John's comment.

EDIT:

In response to the comment about uncalled methods:

Unused methods in ILSpy

The answer is no, that is not the case (compiled for Release).

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • @@Simon : If assembly contains 20 methods out of which only 2 is called and not rest methods, so whether complete assembly get loaded into memory or only this 2 methods – funsukvangdu Jan 22 '13 at 03:24
  • I know that in C++ uncalled methods are simply removed from the executible during compile time. Is this not the case in C#? – Patashu Jan 22 '13 at 03:24
  • Very surprising! I guess it has to guarantee reflecting a private method will work, even when nothing is calling the private method? – Patashu Jan 22 '13 at 03:33
  • @Patashu That sounds about right. It's hard to know for certain a method is called when you can call it via a string literal/variable :) – Simon Whitehead Jan 22 '13 at 03:36
1

Unused code has very little impact on performance. The impact on maintainability however is another topic. Code should be managed for maintainability, readability and understandability first. If the code isn't used and isn't likely to be used, get rid of it as it will reduce all three of those measures.

Address performance when and where you know you have issues because you can measure them.

dkackman
  • 15,179
  • 13
  • 69
  • 123
  • +1. Unused code impacts developers productivity since it needs to be supported, looked at... as result overal performance of an application goes down as developers have less time to add needed features and analize code that is actively used. – Alexei Levenkov Jan 22 '13 at 05:29
1

When Microsoft Released Visual Studio 2012 they include features where you can analyze your code to help you optimize it. Some important Rules:

CA1804: Remove unused locals. Unused local variables and unnecessary assignments increase the size of an assembly and decrease performance.

CA1809: Avoid excessive locals. A common performance optimization is to store a value in a processor register instead of memory, which is referred to as "enregistering the value". To increase the chance that all local variables are enregistered, limit the number of local variables to 64.

It's good to optimize your code including getting rid of unused code but before you do that always have a backup copy. Some of the unused code might be used again for the future. To see what else you can do to optimize your code for better performance. visit the following links

http://msdn.microsoft.com/en-us/library/ms182260.aspx

http://msdn.microsoft.com/en-us/library/dd380629.aspx

http://msdn.microsoft.com/en-us/library/ms182125.aspx

http://msdn.microsoft.com/en-us/library/ms182324.aspx

Jobert Enamno
  • 4,403
  • 8
  • 41
  • 63
0

This post should answer your question. The compiler should ignore any unused using statements. Even if you are using part of a namespace so it is used during compilation it will just copy it in and will only affect the size of your lib/exe and not the performance.

Community
  • 1
  • 1
Nashibukasan
  • 2,028
  • 23
  • 37