Is there a performance difference between TextWriter.WriteLine(String)and TextWriteLine(String, arra

  • Thread starter Thread starter Author
  • Start date Start date
A

Author

I have always been wondering if there is any significant different
between doing

System.Console.WriteLine("Employee Name = " + employee.FirstName + " "
+ employee.LastName);

and

System.Console.WriteLine("Employee Name = {0} {1}",
employee.FirstName, employee.LastName);

Is it a matter of preference or is there a performance issue with the
first version rising from creating immutable strings through
concatenation?

Thank you.
 
Author said:
I have always been wondering if there is any significant different
between doing

System.Console.WriteLine("Employee Name = " + employee.FirstName + " "
+ employee.LastName);

and

System.Console.WriteLine("Employee Name = {0} {1}",
employee.FirstName, employee.LastName);

Is it a matter of preference or is there a performance issue with the
first version rising from creating immutable strings through
concatenation?

The first version is likely to be faster than the second. The first one
just calls string.Concat(string, string, string, string). The second
needs to parse the format string, work out the placeholders and then do
the concatenation.

However, all of this is likely to be dwarfed by the cost of actually
writing to the console. As always, write for readability first and if
you find there's a performance problem, measure, measure, measure!
 
The first version is likely to be faster than the second. The first one
just calls string.Concat(string, string, string, string). The second
needs to parse the format string, work out the placeholders and then do
the concatenation.

However, all of this is likely to be dwarfed by the cost of actually
writing to the console. As always, write for readability first and if
you find there's a performance problem, measure, measure, measure!

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com

OK, thank you very much. Just curious, why do they introduce the
second version (the one using String.Format), which is sorta like the
tradtional C syntax of printf?
 
All you have done is considered one of the the most simplistic forms of a
'formatting' overload of the Console.WriteLine method.

Consider:

Console.WriteLine("Employee Name = {0,-30} {1,30}", employee.FirstName,
employee.LastName);

which gives specific alignment the output.

Consider also the myriad of formatting options you have for numeric values.

Try simulating those with simple string concatenation.


The first version is likely to be faster than the second. The first one
just calls string.Concat(string, string, string, string). The second
needs to parse the format string, work out the placeholders and then do
the concatenation.

However, all of this is likely to be dwarfed by the cost of actually
writing to the console. As always, write for readability first and if
you find there's a performance problem, measure, measure, measure!

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com

OK, thank you very much. Just curious, why do they introduce the
second version (the one using String.Format), which is sorta like the
tradtional C syntax of printf?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top