在.Net中,出于性能原因,何时应该通过引用传递结构? [英] In .Net, when if ever should I pass structs by reference for performance reasons?

查看:69
本文介绍了在.Net中,出于性能原因,何时应该通过引用传递结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#应用​​程序中,我有一个大的结构(176个字节),每秒可能将十万次传递给一个函数.然后,此函数仅获取指向该结构的指针,并将该指针传递给非托管代码.函数和非托管代码都不会对该结构进行任何修改.

In my C# application, I have a large struct (176 bytes) that is passed potentially a hundred thousand times per second to a function. This function then simply takes a pointer to the struct and passes the pointer to unmanaged code. Neither the function nor the unmanaged code will make any modifications to the struct.

我的问题是,我应该按值还是按引用将结构传递给函数?在这种特殊情况下,我的猜测是通过引用传递比将176个字节推入调用堆栈要快得多,除非JIT恰好认识到该结构从未被修改过(我猜是因为该结构的地址传递给非托管代码)并优化代码.

My question is, should I pass the struct to the function by value or by reference? In this particular case, my guess is that passing by reference would be much faster than pushing 176 bytes onto the call stack, unless the JIT happens to recognize that the struct is never modified (my guess is it can't recognize this since the struct's address is passed to unmanaged code) and optimizes the code.

既然可以了,我们还要回答更普遍的情况,即函数 not 不会将结构体的指针传递给非托管代码,而是对对象的内容执行一些只读操作结构.通过引用传递结构会更快吗?在这种情况下,JIT是否会认识到该结构从未被修改过并因此进行了优化?大概通过引用传递1个字节的结构不是效率更高,但是如果通过引用传递结构,则在什么大小的结构上变得更好呢?

Since we're at it, let's also answer the more general case where the function does not pass the struct's pointer to unmanaged code, but instead performs some read-only operation on the contents of the struct. Would it be faster to pass the struct by reference? Would in this case the JIT recognize that the struct is never modified and thus optimize? Presumably it is not more efficient to pass a 1-byte struct by reference, but at what struct size does it become better to pass a struct by reference, if ever?

谢谢.

如下所述,还可以创建一个等价"类以供常规使用,然后在传递给非托管代码时使用结构.我在这里看到两个选项:

As pointed out below, it's also possible to create an "equivalent" class for regular use, and then use a struct when passing to unmanaged code. I see two options here:

1)创建一个仅包含该结构的包装"类,然后在需要时将指向该结构的指针固定并传递给非托管代码.我看到一个潜在的问题是,固定会影响其性能.

1) Create a "wrapper" class that simply contains the struct, and then pin and pass a pointer to the struct to the unmanaged code when necessary. A potential issue I see is that pinning has its own performance consequences.

2)创建一个等效类,当需要该结构时,将其字段复制到该结构中.但是复制会花费很多时间,而且在我看来,首先要克服通过引用传递的观点.

2) Create an equivalent class whose fields are copied to the struct when the struct is needed. But copying would take a lot of time and seems to me to defeat the point of passing by reference in the first place.

正如下面几次提到的,我当然可以衡量每种方法的性能.我执行此操作并发布结果.但是,我仍然有兴趣从理性的角度看待人们的回答和推理.

As mentioned a couple times below, I could certainly just measure the performance of each of these methods. I will do this and post the results. However, I am still interested in seeing people's answers and reasonings from an intellectual perspective.

推荐答案

我做了一些非常非正式的分析,结果表明,对于我的特定应用程序,通过引用传递的性能有所提高.对于by-value,我每秒约有1,050,000个呼叫,而对于by-reference,我每秒约有1,120,000个呼叫.

I did some very informal profiling, and the results indicate that, for my particular application, there is a modest performance gain for passing by reference. For by-value I got about 10,050,000 calls per second, whereas for by-reference I got about 11,200,000 calls per second.

您的里程可能会有所不同.

Your mileage may vary.

这篇关于在.Net中,出于性能原因,何时应该通过引用传递结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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