在C#中使用PARAMS成本 [英] Cost of using params in C#

查看:142
本文介绍了在C#中使用PARAMS成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁使用PARAMS在C#中的方法参数传递有意见。我考虑做重载第6个参数,然后使用PARAMS功能的第7位。我的理由是,以避免额外的数组分配的功能PARAMS要求。这对于一些高高性能的实用方法。任何建议?它是一个代码浪费产生的所有重载?

Does anyone have advice for using the params in C# for method argument passing. I'm contemplating making overloads for the first 6 arguments and then a 7th using the params feature. My reasoning is to avoid the extra array allocation the params feature require. This is for some high performant utility methods. Any advice? Is it a waste of code to create all the overloads?

推荐答案

老实说,我有点被大家喊打扰早熟优化!这里的原因。

Honestly, I'm a little bothered by everyone shouting "premature optimization!" Here's why.


  1. 您说的是什么是非常合情合理的,的尤其的,你已经表明你正在处理的高性能库。

  2. 即使BCL类遵循这种模式。考虑的String.Format Console.WriteLine

  3. 所有的重载这就是很容易得到正确。运动的的背后的整个前提过早的优化是,当你做的东西的棘手的用于优化性能的目的,你容易意外碰坏,使你的代码更少的维护。我看不出这是这里的危险;它应该是非常简单的,你在做什么,给自己以及未来的任何开发谁可以处理你的代码。

  1. What you say makes perfect sense, particularly as you have already indicated you are working on a high-performance library.
  2. Even BCL classes follow this pattern. Consider all the overloads of string.Format or Console.WriteLine.
  3. This is very easy to get right. The whole premise behind the movement against premature optimization is that when you do something tricky for the purposes of optimizing performance, you're liable to break something by accident and make your code less maintainable. I don't see how that's a danger here; it should be very straightforward what you're doing, to yourself as well as any future developer who may deal with your code.

另外即使你的异形的的两种方法的结果和速度只看到一个非常小的差异,还是有内存分配的问题。创建一个新的数组为每个方法调用限嗣继承分配更多的内存,将需要进行垃圾收集后。而在近实时行为需要某些情况下(比如算法交易,外地的的中),尽量减少垃圾收集是一样最大化的执行速度一样重要。

Also, even if you profiled the results of both approaches and saw only a very small difference in speed, there's still the issue of memory allocation. Creating a new array for every method call entails allocating more memory that will need to be garbage collected later. And in some scenarios where "nearly" real-time behavior is desired (such as algorithmic trading, the field I'm in), minimizing garbage collections is just as important as maximizing execution speed.

所以,哪怕挣我一些downvotes:我说去了。

So, even if it earns me some downvotes: I say go for it.

(对于那些谁声称编译器肯定已经做这样的事情。 - 我不会这么肯定首先,如果是这样的话,我不明白为什么BCL类将遵循这种模式,因为我已经提及,但更重要的是,有一个允许的多个参数的方法和1之间,它接受的阵列的。只是因为某人的可以的作为一个非常大的语义差别替代其他并不意味着编译器,或的,试图这样的替代)。

(And to those who claim "the compiler surely already does something like this"--I wouldn't be so sure. Firstly, if that were the case, I fail to see why BCL classes would follow this pattern, as I've already mentioned. But more importantly, there is a very big semantic difference between a method that accepts multiple arguments and one that accepts an array. Just because one can be used as a substitute for the other doesn't mean the compiler would, or should, attempt such a substitution).

这篇关于在C#中使用PARAMS成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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