473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is there a performance difference between TextWriter.WriteLine(String)and TextWriteLine(String, array<Object>[])?

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.
Jun 27 '08 #1
3 3197
Author <gn********@gmail.comwrote:
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!

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
On Jun 23, 2:11*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Author <gnewsgr...@gmail.comwrote:
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!

--
Jon Skeet - <sk...@pobox.com>
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?
Jun 27 '08 #3
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.
"Author" <gn********@gmail.comwrote in message
news:8f**********************************@34g2000h sh.googlegroups.com...
On Jun 23, 2:11 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Author <gnewsgr...@gmail.comwrote:
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!

--
Jon Skeet - <sk...@pobox.com>
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?

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
3
by: David | last post by:
hello... i've a little problem here... n00b question -)) so if you can help me... the "output" string bellow, comes in UNICODE, but i want to get it on windows-1251 (cytillic) how can i do...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
13
by: Murat Ozgur | last post by:
Hello, Is there any difference between ArrayList and List<object? Which one should I use ? Thanks.
9
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus,...
12
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses...
3
by: Sin Jeong-hun | last post by:
If I use something like this, string html = "<h1>C# is great</h1>"; Console.WriteLine(html.Replace("&lt;","<").Replace("&gt;",">")); Does this recreate new string objects two times, even though it...
6
by: Paul.N.Phillips | last post by:
I am using a static dictionary to objects (like cache) but woundered if it is better to use cache. Which one would should I use?
4
by: =?Utf-8?B?a2s=?= | last post by:
Personally I do not like the "sophisticated" code but, anyway I have this (I squeez it to save a space): using System; using System.Collections.Generic; public class SamplesArray { public...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.