什么时候结构太大? [英] when is a struct too big?

查看:101
本文介绍了什么时候结构太大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Structure Vs Class in C#


开放式问题,但我只是想知道如果我的结构是太大。我想使用结构体的原因是因为我已经知道他们比类更快,我真的需要速度,我想。



我发现如果你的结构太大了,它实际上会减慢你的程序,所以我想知道什么是指南,以及我的结构是否需要

  public struct Tiles 
{
public Rectangle rect;

//我知道这不会运行与他们像这样初始化,但如果我改变一个类,这将
//保持像这样和im在决定什么做的中间

位图currentPic =新的位图(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} b 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;
}
}

我应该将它更改为类吗?它对我来说有点。肿。我使用的是Visual Studio 2008.

解决方案

对非黑客程序员的建议:不要优化你的代码。 >

建议黑客程序员:稍后优化您的代码。



认真 advice!



为了给你一些更相关的解释,你应该知道,不像类,结构体是分配在堆栈上,而不是堆上。堆栈分配稍微便宜,但通常只适用于小型对象,它们是短期的,或者是通过值传递的,或者要存储在大型数组中。



如果你不知道通过value意味着什么,这意味着每当你将一个参数传递给一个方法时,整个结构被复制。如果你的结构体不是很小,这可能很快变得非常昂贵。如果您不理解这种区别,它也可能导致意外的行为:对方法调用者可以看到对方法中的参数所做的更改;对方法调用者的 struct 参数所做的更改不是可见的(因为该方法正在更改原始数据的副本)。



要重申我的第一点:不要使用struct,除非你确定你知道你在做什么。



希望这有助于!


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!

这篇关于什么时候结构太大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆