Hi Steven,
Here is my solution...
long st = DateTime.Now.Ticks;
for(int i = 1; i <= 1000000; i++)
{
string sval = "4.23";
int v1 = 0;
int v2 = 0;
int s = sval.IndexOf('.');
if (s < 0)
{
int len = sval.Length;
for(int j = 0; j < len; j++) v1 = (v1 * 10) + ((int) sval[j] - 48);
}
else
{
int len = sval.Length;
for(int j = 0; j < s; j++) v1 = (v1 * 10) + ((int) sval[j] - 48);
for(int j = s + 1; j < len; j++) v2 = (v2 * 10) + ((int) sval[j] - 48);
}
//Console.WriteLine(v1);
//Console.WriteLine(v2);
}
long et = DateTime.Now.Ticks;
Console.WriteLine("Time: {0}",(et - st)/10000);
My approach has 0 Gen 0 collections, no heap memory and 10 times (1000%)
faster than other 2 solutions. Here are the stats taken from Windows Profiler
my 1.7 GHz single processor WIn 2K machine with 1 GB RAM.
Parse static method
-------------------------
445678.989 - 1 million iterations took 2300 milliseconds - 200 (Gen 0
collections) - 62 KB (Bytes in all Heaps)
445678 - 1 million iterations took 1200 milliseconds - 90 (Gen 0
collections) - 62 KB (Bytes in all Heaps)
4.4 - 1 million iterations took 1800 milliseconds - 200 (Gen 0 collections)
- 62 KB (Bytes in all Heaps)
SubString
------------
445678.989 - 1 million iterations took 1400 milliseconds - 80 (Gen 0
collections) - 32 KB (Bytes in all Heaps)
445678 - 1 million iterations took 600 milliseconds - 0 (Gen 0 collections)
- 32 KB (Bytes in all Heaps)
4.4 - 1 million iterations took 1200 milliseconds - 80 (Gen 0 collections) -
32 KB (Bytes in all Heaps)
My approach
----------------
445678.989 - 1 million iterations took 240 milliseconds - 0 (Gen 0
collections) - 0 KB (Bytes in all Heaps)
445678 - 1 million iterations took 175 milliseconds - 0 (Gen 0 collections)
- 0 KB (Bytes in all Heaps)
4.4 - 1 million iterations took 95 milliseconds - 0 (Gen 0 collections) - 0
KB (Bytes in all Heaps)
This is with .Net 1.1. I'll post you my results for .Net 2.0 soon.
Regards,
Aditya.P
"Steven Blair" wrote:
Hi,
I get passed a string like these examples:
3.1
4.23
1.1
5
I need to seperate the values into two integer variables.
So 3.1 would become
int var1, var2;
//var1 should have 3
//var2 should have 1
if it was 4.23
//var1 should have 4
//var2 should have 23
This is will be on a part of the system, where performance is critical
and need an efficient method for processing strings like this.
Thanks for the help in advance.
Steven
*** Sent via Developersdex http://www.developersdex.com ***