19

I find the var keyword greatly helps in reducing noise in my C# code, with little loss of readability; I'd say that I now use explicit typing only when the compiler forces me to.

I know that using var does not change the runtime characteristics of my code. But the question has just occurred to me: am I paying a big penalty at compile time for all the extra work that the compiler is now doing on my behalf?

Has anybody done any benchmarks to see how much difference extensive use of var makes to compilation times?

Community
  • 1
  • 1
Samuel Jack
  • 32,712
  • 16
  • 118
  • 155
  • 1
    Do you have any evidence or reasoning for that answer? – Samuel Jack Feb 18 '10 at 10:31
  • 1
    I erroneously voted for closing (but I cannot take my vote back). The other question is related to some extent, but not exact duplicate. – David Rodríguez - dribeas Feb 18 '10 at 10:38
  • In order to notice a difference the project would have to be so large you would have huge compile times anyway. You would be trying to solve the problem of the 15 min compile and not care about the var keyword. Type inference is one of the main features of F# and there is no measurable difference that I've ever noticed in an F# project that declares NO types. This question is silly imo. – Brian Leahy Feb 18 '10 at 15:56
  • I feel this question has not been answered. The compiler obviously performs differently when you use var, until someone does some bench-marking, or come up with accurate reasoning on the impact it would have, this question should have remained open. – Didier A. Oct 25 '12 at 18:42
  • similar question http://stackoverflow.com/questions/356846/c-sharp-var-vs-specific-type-performance – SMUsamaShah Nov 18 '12 at 15:23

4 Answers4

20

My advice: try it both ways. Measure the results. Then you'll know.

I haven't done any benchmarks, and even if I had, that wouldn't answer the question for you. We do not know what hardware you have, what else is running on your machine, what a typical program looks like. Nor do we know what you consider to be acceptable or unacceptable performance. You're the only one who knows all that, so you're the only one who can answer this question.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 3
    @didibus: I encourage you to post an answer that you like better. – Eric Lippert Nov 19 '12 at 17:10
  • 1
    In retrospect, I see I was a bit out of touch. I just wish the OP would keep this question unanswered so that maybe eventually, someone who does have actual benchmark data or reasoning towards the performance implications on the compiler would come share them here. Or if the OP actually followed this answer, and performed benchmarks of his own, if he could share the results here. – Didier A. Nov 21 '12 at 15:53
16

The types need to be checked anyway, this may even save time... ok, unlikely :)
You shouldn't care though - if your development environment is slow, buy more memory or a new computer. Don't change the way you write code.

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • +1 ... and compiling C# is blazing fast if you compare to other languages like C or C++ – David Rodríguez - dribeas Feb 18 '10 at 10:35
  • I appreciate the point: I'm not going to change my programming practice because of this - but I would like some specifics on the difference it makes. – Samuel Jack Feb 18 '10 at 10:39
  • Do you realize how many different things the compiler does for you already? `var` is just another tiny task... – Kobi Feb 18 '10 at 10:42
  • 3
    +1 for "may even save time". There is no reason to assume that type inference costs time; in fact, it should be 1.271 femtoseconds faster to just _use_ the type on the right side than to check if that type is assignable to the left side. – Erich Kitzmueller Feb 18 '10 at 10:44
  • 1
    Plus parsing the token `var` from text will be faster than parsing the token `IDictionary>`, saving yet another 0.431 femtoseconds. – Mark Byers Feb 19 '10 at 08:41
3

The correct answer is "nothing measurable". For a partial (yet LONG) list of the passes the C# compiler makes while compiling, look here:

http://blogs.msdn.com/ericlippert/archive/2010/02/04/how-many-passes.aspx

Then understand that the type inference is only part of a single pass on that list.

Addys
  • 2,461
  • 15
  • 24
  • The length of the list doesn't really say anything about the relative temporal weights of each item, though. – Joey Jan 17 '12 at 09:21
1

The type of the right hand side needs to be found anyway to do type checking and/or type conversion. Assigning the result to the variable's type is cheap. Most of the cost (if any) will be in what had to be done to allow the expression to be evaluated before all the local variables were declared but you pay for this even if you don't use var. (BTW, it's possible or even likely that the above constraint doesn't hurt performance at all.)

BCS
  • 75,627
  • 68
  • 187
  • 294