On Jul 3, 5:30*pm, Nico VanHaaster <nvanhaas...@caitele.comwrote:
<snip>
Here's the scenario.
I fill a dataAdapter with up to 64 columns (but as low as 10) of data,
with +-300 rows. 63 of the 64 columns are all numbers, doubles, &
int's.
<snip>
The whole process takes the web app about 1.5 minutes to complete. Now
comes my question.
What is the fastest (best performing) way of converting a string to a
percent, double, int then back to a string to display on the page.
It's going to be irrelevant. Let's keep the numbers simple - let's
call it 1000 rows of 100 numbers. Here's a quick test app to generate
all the numbers, then start a timer, convert them to string, parse
them, then format them again:
using System;
using System.Diagnostics;
class Test
{
static void Main()
{
Random rng = new Random();
double[] data = new double[100*1000];
for (int i=0; i < data.Length; i++)
{
data[i] = rng.NextDouble();
}
Stopwatch sw = Stopwatch.StartNew();
int totalLength = 0;
foreach (double datum in data)
{
string simpleFormat = datum.ToString();
double reparsed = double.Parse(simpleFormat);
string reformatted = reparsed.ToString("P2");
// Make absolutely sure the JIT isn't optimising it all away
totalLength += reformatted.Length;
}
sw.Stop();
Console.WriteLine("Time: {0}ms", sw.ElapsedMilliseconds);
Console.WriteLine("Length: {0}", totalLength);
}
}
Now, I'm currently writing this post on an Eee PC (701G). It's running
at 630MHz - hardly a speedy beast. The above (which deals with more
than 5 times as much data as you mentioned earlier) takes about 650ms
to execute. In other words, if the whole process took 1.5 minutes on
this machine, the parsing and reformatting would take less that 1% of
the total.
Basically, don't worry about it from a performance point of view. From
a readability point of view, however, it would be good to avoid doing
lots of extra parsing etc. If you can just cast to an appropriate data
type from your data set (or use a strongly-typed data set to start
with) that would make your code cleaner. Depending on the data, you
might also want to consider using decimal instead of double.
Jon