This question already has answers here:
Closed 8 years ago.
Possible Duplicate:
Structure Vs Class in C#
Sorry if this is a open ended question, but I just want to know if my struct is too big. The reason why i want to use a struct is because I've learned that they're faster then classes and I really need the speed, I think.
I found out that if your struct is too big it actually slows down your program, so I want to know whats the guideline on this and whether or not my struct needs to be converted to a class or not.
public struct Tiles
{
public Rectangle rect;
//i know this wont run with them being initalized like this but if i change to a class this will
//stay like this and im in the middle of deciding what to do
Bitmap currentPic = new Bitmap(50, 50);
ImageAttributes imgAttr = new ImageAttributes();
float[][] ptsArray;
ColorMatrix clrMatrix;
public void setTiles(int i, int k, int width, int height)
{
Rectangle temp = new Rectangle(i, k, width, height);
rect = temp;
float[][] ptsTemp ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, .9f, 0},
new float[] {0, 0, 0, 0, 1}};
ptsArray = ptsTemp;
clrMatrix = new ColorMatrix(ptsArray);
currentPic = The_Great_Find__Dig_Deeper.Properties.Resources.darknessflashlight;
}
public void setTransperancy(float value)
{
clrMatrix.Matrix33 = value;
}
public void drawRect(Graphics g)
{
imgAttr.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(currentPic, rect, 0, 0, currentPic.Width, currentPic.Height, GraphicsUnit.Pixel, imgAttr);
}
bool blackened
{
get;
set;
}
}
Should I change it to a class? It's getting kinda bloated to me. I'm using Visual Studio 2008.
Advice to non-blackbelt programmers: don't optimise your code.
Advice to blackbelt programmers: optimise your code later.
I seriously recommend you heed this advice!
To give you some more pertinent explanation, you should be aware that, unlike classes, structs are allocated on the stack, not on the heap. Stack allocation is slightly cheaper, but is usually only appropriate for small, objects that are either short-lived or which are passed by value or which are going to be stored in a large array.
If you don't know what passed by value means, it means that the entire struct is copied whenever you pass it as an argument to a method. This can quickly become very expensive if your structs are not very small. It can also lead to unexpected behaviour if you don't understand this distinction: changes made to a class argument in a method are visible to the method caller; changes made to a struct argument in a method are not visible to the method caller (because the method is changing a copy of the original data).
To reiterate my first point: don't use structs unless you're sure you know what you're doing.
Hope this helps!
Size isn't the reason to choose a struct over a class. Use struct ONLY if you want value semantics. Think about an int; 5 is a 5 is a 5, references don't matter. structs are always passed by making a copy.
Use a struct when you want value semantics. Otherwise, use a class.
Preemptive optimization is the root of all evil. Or so the saying goes. Build your application, and only then, if it has performance problems try to fix them. For example, one method may run twice as slow as another method, but you can parallize the first and run 4 at a time. Therefore the first method is actually faster. You have to fully understand your performance problem before you can fix it.
To answer your question: From what I recall a struct of size 16 bytes or larger is slower than a class when passed to a method.
Could you please define "speed"?
If the program runs in 10 sec using classes and the same program takes 9.4 sec using structs does it speed up the program or is it a conceivable speed?
You also have to remember that copying will be longer for struct compared to a reference type.
You are the one who can exactly determine if a Struct or Class serves your purpose.
Use Struct if
- Object is Small.
- Logically immutable.
If I were you I would rather use a profiler to exactly know where exactly the program is running slow.
As far as I know, you can not achieve a conceivable improvement using struct instead of class.