i would like to understand if you please help with your opinion on this code below which is the simplest example to the performance issue using .net built-in methods.. and they're highly recommended by online tutorials ...
public void stringFormatVsConcatShowResults()
{
float timeFormat = 0, timeConcat = 0;
for (int counter = 0; counter < 10; i++)
{
if (counter % 2 == 0)
timeFormat += TestStrFormat(1000000, true);
else timeConcat += TestStrFormat(1000000);
}
if (timeFormat > timeConcat)
{
float precWonBy = (timeConcat / timeFormat) * 100;
int prcfractionalPart = new Version(precWonBy.ToString()).Major;
MessageBox.Show("The fstest (by " + prcfractionalPart + " %)is Concat");
}
else
{
float precWonBy = (timeConcat / timeFormat) * 100;
int precfractionalPart = new Version(precWonBy.ToString()).Major;
MessageBox.Show("The fstest (by" + precfractionalPart + " %)is string.Format()");
}
}
public int TestStrFormat(int iterate, bool WstringFormat=false)
{
string testStr = string.Empty, Enewline = Environment.NewLine;
const string header = "its iteration No': ", sign = " !";
Stopwatch swStrFrm = new Stopwatch();
swStrFrm.Start();
if (WstringFormat)
{
for (int counter = 0; counter < iterate; i++)
{
testStr = string.Format("--{0}{1}{2}--{3}", header, counter, sign, Enewline);
}
}
else
{
for (int counter = 0; counter < iterate; i++)
{
testStr = "--its iteration No': " + counter + " !--" + Enewline ;
}
}
swStrFrm.Stop();
return Convert.ToInt32(swStrFrm.ElapsedTicks);
}
what would you choose, and have you notest this performance issue ,
this is only a simple example ,
there's lots of methods like capturing screen in to bitmap and converting an Image to byte[]...
as i am new to software development (4 month - self studying) i have encounterd those situations in multiple junctions, where i need to deside , so How do I choose what methods to use?... am i suppose to be a .net renegade when ever i choose to have max performance in my application or it's(C#) intention wasn't performance to begin with.
just Call stringFormatVsConcatShowResults()
and see for your self what i'm talking about
- ReEdit
so with long strings after that said i would do the following :
public int TestStrWStrBld(int iterate)
{
StringBuilder sb = new StringBuilder();
const string header = "its iteration No': ", sign = " !";
Stopwatch swStrFrm = new Stopwatch();
swStrFrm.Start();
for (int i = 0; i < iterate; i++)
{
sb.Append(header).Append(i).Append(sign).Append(Environment.NewLine);
}
swStrFrm.Stop();
return Convert.ToInt32(swStrFrm.ElapsedTicks);
}
it was now that StringBuilder made it over 600% faster
thanks for opening my mind, from now on ...i'll keep searching for the right implementations.
- ReEdit2 or for smaller strings : compared to string
Str = "a" + "b" + "c";
the folowing does it even 800% faster !!
public int TestStrWStrBld(int iterate)
{
StringBuilder sb = new StringBuilder();
const string header = "its iteration No': ", sign = " !";
string str = string.Empty;
Stopwatch swStrFrm = new Stopwatch();
swStrFrm.Start();
for (int i = 0; i < iterate; i++)
{
str= String.Concat(header, i.ToString(), sign,Environment.NewLine);
}
swStrFrm.Stop();
return Convert.ToInt32(swStrFrm.ElapsedTicks);
}
so i guess the right way is to use String.Concat(string1, string2, etc...)