Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Honestly, I'm a little bothered by everyone shouting "premature optimization!" Here's why.</p> <ol> <li>What you say makes perfect sense, <em>particularly</em> as you have already indicated you are working on a high-performance library.</li> <li>Even BCL classes follow this pattern. Consider all the overloads of <code>string.Format</code> or <code>Console.WriteLine</code>.</li> <li>This is <strong>very easy to get right</strong>. The whole premise behind the movement <em>against</em> premature optimization is that when you do something <em>tricky</em> 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.</li> </ol> <p>Also, even if you <em>profiled</em> 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 <em>I'm</em> in), minimizing garbage collections is just as important as maximizing execution speed.</p> <p>So, even if it earns me some downvotes: I say go for it.</p> <p>(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 <em>multiple arguments</em> and one that accepts an <em>array</em>. Just because one <em>can</em> be used as a substitute for the other doesn't mean the compiler would, or <em>should</em>, attempt such a substitution).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload