Re: StringBuilder
Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
> On the advice of a user, I've timed stringbuilder v string. Here are the
> results.
>
> Here are the numbers:
> Total # queries 3747
> Time in Milliseconds
>
> StringBuilder: String
> 460.6624 320.4608
> 350.504 220.3168
> 240.3456 230.3312
> ----------- -----------
> Ave. 350.504 Ave. 257.0362[/color]
Just looking at these results again, I'm *sure* the bulk of the time is
spent in executing the query. I certainly wouldn't expect it to take
that long just building up the strings.
Just for interest's sake, I ran a test where I built up 100000 strings,
each of the form (hello)(hello)( hello).... (x50) in a similar way to
most of your code. On my (admittedly faster) laptop, that took about
half a second. Here's the code:
using System;
using System.Text;
public class Test
{
public static void Main(string[] args)
{
DateTime start = DateTime.Now;
for (int i=0; i < 100000; i++)
{
StringBuilder sb = new StringBuilder() ;
for (int j=0; j < 50; j++)
{
sb.Append ('(').Append("h ello").Append(' )');
}
}
DateTime end = DateTime.Now;
Console.WriteLi ne (end-start);
}
}
Given those results, I really suspect that very little of your time is
spent actually building the strings.
One thing you *could* do to improve your code is change lines such as:
strTempString.A ppend("'").Appe nd(splitField[RATE]).Append("',");
strTempString.A ppend("'").Appe nd(splitField[SURCHARGE]).Append("',");
strTempString.A ppend("'").Appe nd(splitField[MIN_SEC]).Append("',");
strTempString.A ppend("'").Appe nd(splitField[INCR_SEC]).Append("',");
to:
strTempString.A ppend("'");
strTempString.A ppend(splitFiel d[RATE]).Append("','") ;
strTempString.A ppend(splitFiel d[SURCHARGE]).Append("','") ;
strTempString.A ppend(splitFiel d[MIN_SEC]).Append("','") ;
strTempString.A ppend(splitFiel d[INCR_SEC]).Append("'.");
etc which would reduce the number of appends by a third. I suspect you
won't see much difference though, for the reasons given above.
Out of interest, is there any reason for not using parameterised
queries and then passing in the parameters? I would expect that could
be faster at both the query-building end (with fewer variables
involved) and at the database end.
--
Jon Skeet - <skeet@pobox.co m>
If replying to the group, please do not mail me too
Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
> On the advice of a user, I've timed stringbuilder v string. Here are the
> results.
>
> Here are the numbers:
> Total # queries 3747
> Time in Milliseconds
>
> StringBuilder: String
> 460.6624 320.4608
> 350.504 220.3168
> 240.3456 230.3312
> ----------- -----------
> Ave. 350.504 Ave. 257.0362[/color]
Just looking at these results again, I'm *sure* the bulk of the time is
spent in executing the query. I certainly wouldn't expect it to take
that long just building up the strings.
Just for interest's sake, I ran a test where I built up 100000 strings,
each of the form (hello)(hello)( hello).... (x50) in a similar way to
most of your code. On my (admittedly faster) laptop, that took about
half a second. Here's the code:
using System;
using System.Text;
public class Test
{
public static void Main(string[] args)
{
DateTime start = DateTime.Now;
for (int i=0; i < 100000; i++)
{
StringBuilder sb = new StringBuilder() ;
for (int j=0; j < 50; j++)
{
sb.Append ('(').Append("h ello").Append(' )');
}
}
DateTime end = DateTime.Now;
Console.WriteLi ne (end-start);
}
}
Given those results, I really suspect that very little of your time is
spent actually building the strings.
One thing you *could* do to improve your code is change lines such as:
strTempString.A ppend("'").Appe nd(splitField[RATE]).Append("',");
strTempString.A ppend("'").Appe nd(splitField[SURCHARGE]).Append("',");
strTempString.A ppend("'").Appe nd(splitField[MIN_SEC]).Append("',");
strTempString.A ppend("'").Appe nd(splitField[INCR_SEC]).Append("',");
to:
strTempString.A ppend("'");
strTempString.A ppend(splitFiel d[RATE]).Append("','") ;
strTempString.A ppend(splitFiel d[SURCHARGE]).Append("','") ;
strTempString.A ppend(splitFiel d[MIN_SEC]).Append("','") ;
strTempString.A ppend(splitFiel d[INCR_SEC]).Append("'.");
etc which would reduce the number of appends by a third. I suspect you
won't see much difference though, for the reasons given above.
Out of interest, is there any reason for not using parameterised
queries and then passing in the parameters? I would expect that could
be faster at both the query-building end (with fewer variables
involved) and at the database end.
--
Jon Skeet - <skeet@pobox.co m>
If replying to the group, please do not mail me too
Comment