L
-Lost
Question about performance (very simple example given).
I read in the MSDN Express documentation that when concatenating
large strings that it is actually more efficient to build them with
StringBuilder.
My professor however told me this is wrong, and specifically that I
was wrong for using StringBuilder in this case:
"Build a string that consists of the numbers 1 through 100, no
spaces, no newlines."
So I did this:
StringBuilder strOneToOneHundred = new StringBuilder();
for (int i = 1; i <= 100; i++)
{
strOneToOneHundred.Append(i);
}
Console.Write(strOneToOneHundred);
He told me that in this case it was overkill and then suggested I
"stick to writing to the console." I asked what that meant and he
replied, "just use console write to output it to the console."
Surely he didn't mean:
Console.Write("1");
// 98 more Console.Writes
Console.Write("100");
So, is StringBuilder overkill in this case? Are numerous (as in tens
and tens of them) Console.Write's smarter? More efficient?
And before anyone asks... I was told not to use Arrays.
I read in the MSDN Express documentation that when concatenating
large strings that it is actually more efficient to build them with
StringBuilder.
My professor however told me this is wrong, and specifically that I
was wrong for using StringBuilder in this case:
"Build a string that consists of the numbers 1 through 100, no
spaces, no newlines."
So I did this:
StringBuilder strOneToOneHundred = new StringBuilder();
for (int i = 1; i <= 100; i++)
{
strOneToOneHundred.Append(i);
}
Console.Write(strOneToOneHundred);
He told me that in this case it was overkill and then suggested I
"stick to writing to the console." I asked what that meant and he
replied, "just use console write to output it to the console."
Surely he didn't mean:
Console.Write("1");
// 98 more Console.Writes
Console.Write("100");
So, is StringBuilder overkill in this case? Are numerous (as in tens
and tens of them) Console.Write's smarter? More efficient?
And before anyone asks... I was told not to use Arrays.