473,804 Members | 4,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a performance difference between TextWriter.Writ eLine(String)an d TextWriteLine(S tring, array<Object>[])?

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

System.Console. WriteLine("Empl oyee Name = " + employee.FirstN ame + " "
+ employee.LastNa me);

and

System.Console. WriteLine("Empl oyee Name = {0} {1}",
employee.FirstN ame, employee.LastNa me);

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 3228
Author <gn********@gma il.comwrote:
I have always been wondering if there is any significant different
between doing

System.Console. WriteLine("Empl oyee Name = " + employee.FirstN ame + " "
+ employee.LastNa me);

and

System.Console. WriteLine("Empl oyee Name = {0} {1}",
employee.FirstN ame, employee.LastNa me);

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(s tring, 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.co m>
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.co mwrote:
Author <gnewsgr...@gma il.comwrote:
I have always been wondering if there is any significant different
between doing
System.Console. WriteLine("Empl oyee Name = " + employee.FirstN ame + " "
+ employee.LastNa me);
and
System.Console. WriteLine("Empl oyee Name = {0} {1}",
employee.FirstN ame, employee.LastNa me);
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(s tring, 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.co m>
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.WriteLi ne method.

Consider:

Console.WriteLi ne("Employee Name = {0,-30} {1,30}", employee.FirstN ame,
employee.LastNa me);

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********@gma il.comwrote in message
news:8f******** *************** ***********@34g 2000hsh.googleg roups.com...
On Jun 23, 2:11 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
Author <gnewsgr...@gma il.comwrote:
I have always been wondering if there is any significant different
between doing
System.Console. WriteLine("Empl oyee Name = " + employee.FirstN ame + " "
+ employee.LastNa me);
and
System.Console. WriteLine("Empl oyee Name = {0} {1}",
employee.FirstN ame, employee.LastNa me);
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(s tring, 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.co m>
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
5098
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 are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
3
27952
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 this?.. webResponse = (HttpWebResponse)webRequest.GetResponse(); Stream streamResponse = webResponse.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse);
16
4932
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
13
31256
by: Murat Ozgur | last post by:
Hello, Is there any difference between ArrayList and List<object? Which one should I use ? Thanks.
9
7003
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, (MyClass is object) should always evaluate as true. However, if I have a method with the following signature:
12
2453
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 a Static Object. In this object is a generic List<String>. For every page request this list is looped over only reading not writing each value.
3
1968
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 has nothing to replace, and it is OK to return the same string object? I know, regular expression is a more delicate way to do this, but it always is kind of too complicated to me.
6
7276
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
2329
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 static void Main() {
0
9707
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.