|
-
Dec 19th, 2008, 07:28 PM
#1
Thread Starter
Hyperactive Member
Increasing peformance for C# applications
Hello
books or documents to download for building C# applications for best performance, memory management etc.. how we can improve the performance for C# applications ...
And also which are the best design pattern books especially database driven projects ...
thankzzz
Last edited by sureshvijayan; Dec 19th, 2008 at 07:46 PM.
gh
-
Dec 19th, 2008, 07:50 PM
#2
Re: Increasing peformance for C# applications
You practically dont need to think about memory management in C#, as it makes use of a garbage collector that handles it for you. The only thing to keep in mind is just to dispose items that are disposable, as soon as you dont need them anymore.
Other than that, I dont think you'll find many books or documents specifically on performance boosting, since C# isnt a language designed for high-performance tasks. Basically the main thing that can slow you down is badly written code and algorithms, so thats what you need to focus on. If you need speed then in some rare occasions you may benefit from unsafe code, which you can read about on MSDN.
Last edited by Atheist; Dec 19th, 2008 at 07:54 PM.
-
Dec 23rd, 2008, 12:22 PM
#3
Re: Increasing peformance for C# applications
 Originally Posted by Atheist
You practically dont need to think about memory management in C#, as it makes use of a garbage collector that handles it for you. The only thing to keep in mind is just to dispose items that are disposable, as soon as you dont need them anymore.
While you are correct in regards to the disposable comment I would disagree about not really worrying about memory management.
There are many things you can do to increase performance.
- Limit the use of global variables (best not to have any)
- Initialize items only when you need to
- Do not pass large items by value. Pass them by ref where appropriate
- Use generic containers; ArrayList = BAD
- Use the using statement on all items that inherit from IDisposable to ensure the unmanaged resources are freed as soon as possible
- Use unsafe code for algorithms only if it makes sense (can help increase performance as Atheist said)
There are other things you can do such as code reviews with others to ensure your code is well written and not memory intensive.
Other possibilities are moving algorithms that can take some time to C++.
-
Dec 23rd, 2008, 06:14 PM
#4
Re: Increasing peformance for C# applications
 Originally Posted by kasracer
[*]Do not pass large items by value. Pass them by ref where appropriate
This one should never be an issue. When passing a reference type, i.e. a class, by value you are simply copying the reference, not the object, so the size of the object is no issue.
When passing value types, i.e. structures, by value you do copy the object but structures should never be large enough for this to be an issue anyway. Structures are supposed to be no larger than 16 bytes so if you have a structure that is big enough to make passing by value an issue then you should have implemented it as a class instead.
Also, IDisposable is an interface so classes cannot inherit it, only implement it.
-
Dec 23rd, 2008, 10:06 PM
#5
Fanatic Member
Re: Increasing peformance for C# applications
I have a question in a global variable in C# let say I have 2 classes.
Code:
class1
{
public newThings obj = null;
public object method1()
{
obj = new newThings();
}}
Code:
class1
{
public newThings obj = new newThings():
public object method1()
{
}}
Given the 2 example classes. Is the obj object is a global variable for class1 and class2?
Last edited by popskie; Dec 23rd, 2008 at 10:10 PM.
-
Dec 23rd, 2008, 10:37 PM
#6
Re: Increasing peformance for C# applications
 Originally Posted by popskie
I have a question in a global variable in C# let say I have 2 classes.
Code:
class1
{
public newThings obj = null;
public object method1()
{
obj = new newThings();
}}
Code:
class1
{
public newThings obj = new newThings():
public object method1()
{
}}
Given the 2 example classes. Is the obj object is a global variable for class1 and class2?
In VB, when people use the term "global variable" they generally mean a public variable in a module. The C# equivalent of a VB module is a static class. Any static member of a class, or structure for that matter, is essentially global because they can be accessed anywhere in the project without the need for a reference to an object. There's nothing global about anything in that code.
-
Dec 24th, 2008, 12:44 AM
#7
Re: Increasing peformance for C# applications
 Originally Posted by jmcilhinney
This one should never be an issue. When passing a reference type, i.e. a class, by value you are simply copying the reference, not the object, so the size of the object is no issue.
Hmm... this always confuses me about C#. I'm used to C++ where everything is by value unless specified as a reference. Though practically everything is based on the Object class which makes me wonder if the ref keyword is really that useful.
Are you sure everything is really passed with a copy of the reference though? How does it work when I modify a variable passed in? I've made functions before that took lists, cleaned or updated them and they still contained data outside of the function.
 Originally Posted by jmcilhinney
When passing value types, i.e. structures, by value you do copy the object but structures should never be large enough for this to be an issue anyway. Structures are supposed to be no larger than 16 bytes so if you have a structure that is big enough to make passing by value an issue then you should have implemented it as a class instead.
But structures would be made up of other objects (such as int) that have a base class of Object so wouldn't that mean the structure contains just references? Are there are objects / variables in C# that are not a reference type? I was under the impression that Object was referenced based and everything is based on Object.
 Originally Posted by jmcilhinney
Also, IDisposable is an interface so classes cannot inherit it, only implement it.
Bah, same difference!
-
Dec 24th, 2008, 05:40 AM
#8
Re: Increasing peformance for C# applications
All classes are reference types. All structures are value types. All variables exist on the stack. A reference-type variable contains a reference to an object, i.e. the memory address of an object that exists on the heap. A value-type variable contains a value, i.e. an object itself.
When you pass an argument to a method by value you make a copy of the variable and pass that. If the variable is a reference type then you're making a copy of a reference. If the variable is a value type then you're copying an object.
When you pass an argument to a method by reference you are create a reference to the variable and pass that. If the variable is a reference type you're creating a reference to a reference. If the variable is a value type you're creating a reference to an object.
When you pass a reference type variable to a method then there is only one object, whether you pass by value or by reference. As such, any changes you make to that object within the method, e.g. add items to a collection, will be reflected in the original. Of course they must because there is only one object. The difference is when you actually assign a new object to the parameter within the method. If the argument was passed by value then the original variable will still refer to the original object, while if the argument was passed by reference then the original variable will now refer to the new object.
When passing a value type by value, no changes you make within the method affect the original. When passing a value type by reference, all changes you make within the method affect the original.
If you understand the difference between value types and reference types this is all perfectly logical, so I suggest you brush up on that kas. Try running the following code, which will hopefully shed some light on the subject:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyValueType mvt;
mvt = new MyValueType() { data = "Original Data" };
this.PassValueTypeByValueAndChangeData(mvt);
MessageBox.Show(mvt.data, "PassValueTypeByValueAndChangeData");
mvt = new MyValueType() { data = "Original Data" };
this.PassValueTypeByValueAndChangeObject(mvt);
MessageBox.Show(mvt.data, "PassValueTypeByValueAndChangeObject");
mvt = new MyValueType() { data = "Original Data" };
this.PassValueTypeByReferenceAndChangeData(ref mvt);
MessageBox.Show(mvt.data, "PassValueTypeByReferenceAndChangeData");
mvt = new MyValueType() { data = "Original Data" };
this.PassValueTypeByReferenceAndChangeObject(ref mvt);
MessageBox.Show(mvt.data, "PassValueTypeByReferenceAndChangeObject");
MyReferenceType mrt;
mrt = new MyReferenceType() { data = "Original Data" };
this.PassReferenceTypeByValueAndChangeData(mrt);
MessageBox.Show(mrt.data, "PassReferenceTypeByValueAndChangeData");
mrt = new MyReferenceType() { data = "Original Data" };
this.PassReferenceTypeByValueAndChangeObject(mrt);
MessageBox.Show(mrt.data, "PassReferenceTypeByValueAndChangeObject");
mrt = new MyReferenceType() { data = "Original Data" };
this.PassReferenceTypeByReferenceAndChangeData(ref mrt);
MessageBox.Show(mrt.data, "PassReferenceTypeByReferenceAndChangeData");
mrt = new MyReferenceType() { data = "Original Data" };
this.PassReferenceTypeByReferenceAndChangeObject(ref mrt);
MessageBox.Show(mrt.data, "PassReferenceTypeByReferenceAndChangeObject");
}
private void PassValueTypeByValueAndChangeData(MyValueType mvt)
{
mvt.data = "New Data";
}
private void PassValueTypeByValueAndChangeObject(MyValueType mvt)
{
mvt = new MyValueType() { data = "New Object" };
}
private void PassValueTypeByReferenceAndChangeData(ref MyValueType mvt)
{
mvt.data = "New Data";
}
private void PassValueTypeByReferenceAndChangeObject(ref MyValueType mvt)
{
mvt = new MyValueType() { data = "New Object" };
}
private void PassReferenceTypeByValueAndChangeData(MyReferenceType mrt)
{
mrt.data = "New Data";
}
private void PassReferenceTypeByValueAndChangeObject(MyReferenceType mrt)
{
mrt = new MyReferenceType() { data = "New Object" };
}
private void PassReferenceTypeByReferenceAndChangeData(ref MyReferenceType mrt)
{
mrt.data = "New Data";
}
private void PassReferenceTypeByReferenceAndChangeObject(ref MyReferenceType mrt)
{
mrt = new MyReferenceType() { data = "New Object" };
}
}
public class MyReferenceType
{
public string data;
}
public struct MyValueType
{
public string data;
}
}
Note that that's code from a C# 3.0, .NET 3.5 WinForm.
-
Dec 24th, 2008, 01:04 PM
#9
Re: Increasing peformance for C# applications
 Originally Posted by jmcilhinney
If you understand the difference between value types and reference types this is all perfectly logical,
I understand the difference. What doesn't make sense to me is, as far as I know, every object in the .Net framework is a reference type except a struct but a struct must contain objects. I just don't understand why C# would have value types and reference types when the only value type can contain reference types. Wouldn't it have been better to just have it setup like C++ where functions cannot modify the original variables unless it's setup to accept a reference in the first place?
What's the point of the ref keyword in C# if everything is a reference anyway? Was it meant to only be used with structs?
-
Dec 24th, 2008, 05:41 PM
#10
Re: Increasing peformance for C# applications
-
Dec 24th, 2008, 10:08 PM
#11
Re: Increasing peformance for C# applications
 Originally Posted by kasracer
What's the point of the ref keyword in C# if everything is a reference anyway? Was it meant to only be used with structs?
I was under the impression I'd already answered that. If you pass a reference type by value and assign a new object to the parameter inside the method the original variable will still refer to the ORIGINAL object. If you pass a reference type by reference and assign a new object to the parameter inside the method then the original variable will refer to the NEW object.
In short, reference type arguments passed by value can be edited but not replaced, while reference type arguments passed by reference can be edited AND replaced. This is completely consistent with how value type and reference type variables work in general.
There is no way to pass a reference type variable to a method and prevent the object itself from being changed. If you want to ensure that your original object cannot be changed then you must make a copy of it and pass that to the method.
One of the main reasons that C# is easier to use than C++ is the existence of value types and reference types instead of just using objects and pointers. The fact that you can treat a variable that contains an object and a variable that contains a reference to an object in almost exactly the same way is one of the main reasons that C# is so popular.
Last edited by jmcilhinney; Dec 24th, 2008 at 10:12 PM.
-
Dec 25th, 2008, 12:29 AM
#12
Re: Increasing peformance for C# applications
 Originally Posted by jmcilhinney
In short, reference type arguments passed by value can be edited but not replaced, while reference type arguments passed by reference can be edited AND replaced. This is completely consistent with how value type and reference type variables work in general.
Ah I understand now. Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|