C# 'ref' 关键字,性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/900903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# 'ref' keyword, performance
提问by David Anderson
If you have a Bitmap object that needs to be passed to numerous methods (about 10), and finally to an event where it shall finally be disposed of after it's used, would it be (performance wise) beneficial to pass it to every one of those methods by reference instead of value?
如果您有一个 Bitmap 对象需要传递给许多方法(大约 10 个),最后传递给一个在使用后最终将被处理的事件,将它传递给每个方法是否(性能方面)有益那些方法是通过引用而不是值?
Passing by value, the object is copied, passing by reference its not.
通过值传递,对象被复制,通过引用传递它的不是。
(Performance is critical in this situation. The application needs to run as fast as possible.)
(在这种情况下,性能至关重要。应用程序需要尽可能快地运行。)
采纳答案by David Nelson
Bitmap is a reference type. Passing a reference type by value does not copy the object, merely the reference to the object. There would be no performance benefit to passing the Bitmap by reference instead of by value.
位图是一种引用类型。按值传递引用类型不会复制对象,而只会复制对对象的引用。通过引用而不是通过值传递位图不会有性能优势。
回答by Gishu
Which type are you using exactly for holding the Bitmap? For example, System.Drawing.Bitmapis a reference type/class. When you pass a reference to a method (as an argument), the reference is passed by value. (A copy of the reference is made... not the object) So four bytes would be allocated on a 32-bit machine to hold the copy.
您使用哪种类型来保存位图?例如,System.Drawing.Bitmap是一个引用类型/类。当您传递对方法的引用(作为参数)时,该引用是按值传递的。(引用的副本...不是对象)因此将在 32 位机器上分配四个字节来保存副本。
Using the ref keyword has not much bearing on performance except that the same reference is passed (a copy of the reference is not made). It has the following benefits
除了传递相同的引用(不制作引用的副本)之外,使用 ref 关键字对性能没有太大影响。它有以下好处
- Only clears the intent that the method taking the parameter may modify it, and the caller may get a modified value post execution.
- And the variable must be initialized by the calleebefore being passed as an argument to the called function taking the ref parameter.
- 只清除接受参数的方法可能对其进行修改的意图,调用者可能会在执行后获得修改后的值。
- 并且该变量必须由被调用者初始化,然后才能作为参数传递给采用 ref 参数的被调用函数。
回答by Jeromy Irvine
Since Bitmap is a reference type, there is no practical difference for performance in this scenario as it is already being passed by reference to the method.
由于 Bitmap 是一种引用类型,因此在这种情况下性能没有实际差异,因为它已经通过引用传递给方法。
I'd recommend Jon Skeet's article on the subjectfor a thorough explanation of how "by reference" and "by value" work in C#.
我推荐Jon Skeet 关于这个主题的文章,以全面解释“按引用”和“按值”在 C# 中的工作原理。
回答by Grant Peters
The 'ref' doesn't pass the object itself into the function, instead it passes a reference to the variable it is stored in/at.
'ref' 不会将对象本身传递给函数,而是传递对其存储在/at 中的变量的引用。
If the object is a class or an interface, then whenever you access that variable in the function, it has to dereference it, and then access the variable. If it was passed in without the 'ref' keyword, then it wouldn't have to do the dereference step (thus it will be slightly faster).
如果对象是类或接口,那么无论何时在函数中访问该变量,它都必须取消对它的引用,然后再访问该变量。如果它是在没有 'ref' 关键字的情况下传入的,那么它就不必执行取消引用步骤(因此它会稍微快一点)。
If the object is a struct (or enum or other base type), the 'ref' keyword passes a reference to the variable that stores the struct, which still causes the SLIGHT speed hit of dereferencing whenever you use it, but if you don't specify it, then the program has to allocate memory for a new instance of the struct and then copy it. In most cases, it is faster to pass structs via the ref keyword, but this may not be the case if it is a really small struct, or if it's dereferenced a lot (and I mean a LOT).
如果对象是结构体(或枚举或其他基类型),则“ref”关键字传递对存储结构体的变量的引用,这仍然会导致在使用它时解引用的速度略有下降,但如果不这样做” t 指定它,然后程序必须为结构的新实例分配内存,然后复制它。在大多数情况下,通过 ref 关键字传递结构会更快,但如果它是一个非常小的结构,或者如果它被取消引用很多(我的意思是很多),情况可能并非如此。
So if you are passing a struct, then ref is usually the way to go, otherwise there won't really be much of a difference (the overhead in the dereferencing phase is minuscule).
因此,如果您正在传递一个结构体,那么 ref 通常是要走的路,否则实际上不会有太大的区别(解引用阶段的开销微乎其微)。
Oh, and to actually answer the question, a Bitmap is a class, so the 'ref' keyword won't really make a speed difference over a non-'ref' parameter.
哦,要真正回答这个问题,位图是一个类,因此“ref”关键字不会真正对非“ref”参数产生速度差异。