类VS REF结构体 [英] Class VS ref Struct

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

问题描述

我编程使用C#游戏,因此,我非常关心性能。

I am programming a game using C#, thus, I am very concerned about performance.

我想知道什么是主要区别,如果可能的话,使用一个类来传递数据左右,或者通过引用传递一个结构性能的考虑。

I would like to know what are the main differences, and if possible, performance considerations of using either a Class to pass data around, or a struct passed by reference.

我希望不要将数据身边复制,性能方面的原因(我假设由裁判传球比这里值更快)。

I wish not to copy the data around, for performance reasons (I assume passing by ref is much faster than by value here).

我知道,一类总是由参考和一个结构是按值传递过去了,但我谈论参照路过这里的结构。

我要传递的数据的一个例子:

An example of the data I wish to pass :

    public delegate void PathCompleteDelegate(List<PathFinderNode> path);
public struct PathFinderJob{
    public PathCompleteDelegate callback;
    public Vector3 start, end;
    public PathSize unitSize;
    public BoxCollider boxCollider;
}

在前面的例子中,采用了类有所作为会?如果是这样,将区别是什么?将一个类会比在这个例子中一个结构快?为什么呢?

In the previous example, would using a class make a difference? If so, what would the difference be? Would a class be faster than a struct in this example? Why?

感谢您。
若昂·卡洛斯

Thank you. Joao Carlos

推荐答案

您委托收到引用类型 - 一个列表,所以你通过引用传递反正整个列表。

Your delegate receives a reference type - a List, so you're passing the entire list by reference anyway.

按值传递一个大的结构肯定比仅仅通过参考最昂贵的。当你有一个大的结构,它通常没有意义的,使用它作为一个结构,只是把它变成一个类。

Passing a large structure by value is definitely most expensive than passing just the reference. When you have a large structure, it usually doesn't make sense to use it as a structure, just turn it into a class.

不管怎样,你的确保的你会在这里有一个性能问题?似乎是一个非常不成熟的优化。

Anyway, are you sure you'll have a performance issue here? Seems like a very premature optimization.

这篇关于类VS REF结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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