473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Format Numbers To Strings.. Perfomance?

Hello,

I don't really want to get too much of a debate here, but I have a
feeling this just might cause one. I have been trying to increase the
performance of one of my applications. (C# .Net web app).

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.

Now I then have to loop through the dataAdapter and build a table out
of the data.

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.

Also. What sort of performance implications come from statements such
as Convert.ToDouble(MyDataRow[my column
index].ToString()).ToString("P2");

This seems like pretty basic stuff however I can't find too many
resources out there that deal with these types of problems.

Thank you in advance.
Jul 3 '08 #1
9 2491
Nico,

If you have a specific format that you are converting to/from, then I
imagine that you could write your own specialized routine. However, I don't
know that you are going to do much better than using Parse/TryParse with a
specific format.

In the end, this is easy enough to test, using the Stopwatch class in
the System.Diagnostics namespace.

I find it to be a little strange that it would take 1.5 minutes to
process 300 rows with 64 columns. I wouldn't think to look at the
formatting of the strings as the culprit. Is this what you think? My first
guess would be whatever query/stored procedure you are calling that is
getting the data. If I was looking to enhance the performance of this
operation, that is the first place I would look.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nico VanHaaster" <nv*********@caitele.comwrote in message
news:e0**********************************@m3g2000h sc.googlegroups.com...
Hello,

I don't really want to get too much of a debate here, but I have a
feeling this just might cause one. I have been trying to increase the
performance of one of my applications. (C# .Net web app).

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.

Now I then have to loop through the dataAdapter and build a table out
of the data.

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.

Also. What sort of performance implications come from statements such
as Convert.ToDouble(MyDataRow[my column
index].ToString()).ToString("P2");

This seems like pretty basic stuff however I can't find too many
resources out there that deal with these types of problems.

Thank you in advance.

Jul 3 '08 #2
Nico VanHaaster wrote:
I don't really want to get too much of a debate here, but I have a
feeling this just might cause one. I have been trying to increase the
performance of one of my applications. (C# .Net web app).

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.

Now I then have to loop through the dataAdapter and build a table out
of the data.

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.

Also. What sort of performance implications come from statements such
as Convert.ToDouble(MyDataRow[my column
index].ToString()).ToString("P2");

This seems like pretty basic stuff however I can't find too many
resources out there that deal with these types of problems.
You are looking at the wrong place.

I did a little test and I can convert int->string->int 1.3 millions
times per second on a 3 year old PC.

64 * 300 conversions should be done in 1/100 of a second.

Doubles and decimals will take a bit more time.

But it is not the conversions that are taking your 1.5 minutes.

I think you should start by measuring with a profiler.

Arne
Jul 3 '08 #3
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
Jul 3 '08 #4
Great points guys and you were absolutly correct.

I was using the stop watch feature however was measuring the time
incorrectly. I was doing two things wrong 1) Not converting my
StopWatch.Elapsed to a TimeSpan. 2) When converting to a TimeSpan i
used objTS.Seconds not objTS.TotalSeconds.

The problem is in my selection from the SQL Server which in my mind
makes sense.

Thank you for your help. If anything i know that i can not increase my
performance too much by modifying the way i am converting my
dataTypes.

Jon,
Would you mean by strongly typed datasets as.
DataSet objDS = new DataSet();
DataColumn dc = new DataColumn("x",Type.GetType("System.Double")); //
diferent for each column"
objDS.Columns.Add(dc);
SqlDataAdapter.Fill(objDS);

Thank you again.
Jul 3 '08 #5
Nico VanHaaster laid this down on his screen :
Hello,
<snip>
Also. What sort of performance implications come from statements such
as Convert.ToDouble(MyDataRow[my column
index].ToString()).ToString("P2");
Just a minor point (as the others pointed out, this will not be your
bottleneck): what columntype does MyDataRow[my column index] have?
If it already *is* a double, then you can just cast it to double,
instead of ToString() followed by Convert.ToDouble (which will do a
Parse).

So use
((double)MyDataRow[my column index]).ToString("P2");

Hans Kesting
Jul 4 '08 #6
On Jul 3, 6:26*pm, Nico VanHaaster <nvanhaas...@caitele.comwrote:
Great points guys and you were absolutly correct.

I was using the stop watch feature however was measuring the time
incorrectly. I was doing two things wrong 1) Not converting my
StopWatch.Elapsed to a TimeSpan. 2) When converting to a TimeSpan i
used objTS.Seconds not objTS.TotalSeconds.
Stopwatch.ElapsedMilliseconds is another way of getting the result
pretty easily.
The problem is in my selection from the SQL Server which in my mind
makes sense.
Yes - when there's a database involved, local computation is rarely
(but sometimes) the bottleneck.

<snip>
Would you mean by strongly typed datasets as.
DataSet objDS = new DataSet();
DataColumn dc = new DataColumn("x",Type.GetType("System.Double")); //
diferent for each column"
objDS.Columns.Add(dc);
SqlDataAdapter.Fill(objDS);

Thank you again.
Not quite - I mean going through the designer which creates a strongly
typed dataset for you. MSDN has a lot of information on the topic,
IIRC - I can't say I've used datasets of either description very much.

Jon
Jul 4 '08 #7


"Nico VanHaaster" <nv*********@caitele.comwrote in message
news:80**********************************@c65g2000 hsa.googlegroups.com...
Great points guys and you were absolutly correct.

I was using the stop watch feature however was measuring the time
incorrectly. I was doing two things wrong 1) Not converting my
StopWatch.Elapsed to a TimeSpan. 2) When converting to a TimeSpan i
used objTS.Seconds not objTS.TotalSeconds.

The problem is in my selection from the SQL Server which in my mind
makes sense.

Thank you for your help. If anything i know that i can not increase my
performance too much by modifying the way i am converting my
dataTypes.

Jon,
Would you mean by strongly typed datasets as.
DataSet objDS = new DataSet();
DataColumn dc = new DataColumn("x",Type.GetType("System.Double")); //
diferent for each column"
objDS.Columns.Add(dc);
SqlDataAdapter.Fill(objDS);

Thank you again.

A strongly typed dataset is not what you have above. Check out the MSDN
documentation on typed datasets, or google if you prefer. As for creating
columns as above, even though it's not strongly typed datasets, you should
still use:

DataColumn dc = new DataColumn("x", typeof(double));

Notice the typeof call instead of Type.GetType.

HTH,
Mythran
Jul 7 '08 #8


"Nico VanHaaster" <nv*********@caitele.comwrote in message
news:e0**********************************@m3g2000h sc.googlegroups.com...
Hello,
<snip>
Also. What sort of performance implications come from statements such
as Convert.ToDouble(MyDataRow[my column
index].ToString()).ToString("P2");

This seems like pretty basic stuff however I can't find too many
resources out there that deal with these types of problems.

Thank you in advance.
Instead of:

Convert.ToDouble(MyDataRow[index].ToString()).ToString("P2")

If the column at the specified index is stored as a double, just cast it
directly:

((double) MyDataRow[index]).ToString("P2")

This is a direct cast instead of a conversion. Should be faster, but I have
no definitive tests to show to prove it.

HTH,
Mythran
Jul 7 '08 #9


"Mythran" <My*****@community.nospamwrote in message
news:51**********************************@microsof t.com...
>

"Nico VanHaaster" <nv*********@caitele.comwrote in message
news:e0**********************************@m3g2000h sc.googlegroups.com...
>Hello,

<snip>
>Also. What sort of performance implications come from statements such
as Convert.ToDouble(MyDataRow[my column
index].ToString()).ToString("P2");

This seems like pretty basic stuff however I can't find too many
resources out there that deal with these types of problems.

Thank you in advance.

Instead of:

Convert.ToDouble(MyDataRow[index].ToString()).ToString("P2")

If the column at the specified index is stored as a double, just cast it
directly:

((double) MyDataRow[index]).ToString("P2")

This is a direct cast instead of a conversion. Should be faster, but I
have no definitive tests to show to prove it.

HTH,
Mythran

BTW, the results of my posts are not a "significant" improvement in
performance. Over a period of a million rows, you "may" see a 1 or 2 second
difference in processing time...if the conversions and to strings are all
that you are doing in the loops...but since you are only converting a couple
thousand values in the loop, you probably won't see much of a
difference....just a note since I didn't mention it in my last replies.

HTH,
Mythran

Jul 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
7
by: Sobhan | last post by:
Hi all, Iam writing a small C program to print a array of numbers in Comma Separated Values in VC++ editor. Lets say: Title Title1 Title2 ----- ------ ------ 10,20,30 50,90,100 120,180,300
11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
4
by: Jan | last post by:
Is there a way to display numbers in specified format in data grid. I know that I can do this converting numbers to strings but in the grid I want them as numbers (because of sorting). For...
1
by: nic | last post by:
Hi, I have a textbox value that I need to force (and format) to be a numeric value. For example, I want to allow a user to type 1000 or 1,000 or 1,000,000 but not something that is not numberic....
3
by: Gary Wessle | last post by:
Hi I wrote 2 functions "included at the bottom" one inserts and one reads data from a file, the data format is as here **************** a 2 b -0.0336974 _time 1.17312e+09 ****************
7
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.