473,395 Members | 1,972 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,395 software developers and data experts.

Efficient way to do this

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 ***
Oct 17 '06 #1
4 1492
How about something like:
private static int[] Parse(string value) {
string[] elements = value.Split('.');
int length = elements.Length;
int[] values = new int[length];
for (int i = 0; i < length; i++) {
values[i] = int.Parse(elements[i]);
}
return values;
}

If you know there are never more than 2 parts, then you might be able to
tweak bits of it, but doubt it would be worth it...

Marc
Oct 17 '06 #2
Efficient depends on various parameters like speed, memory usage ecc.
I'd use something like this:

string sval = "44.55";
int v1, v2;

int s = sval.IndexOf('.'); // linear but small strings
if (s < 0) // Case no decimals
{
v1 = Convert.ToInt32(sval); // full integer
}
else
{
v1 = Convert.ToInt32(sval.Substring(0, s)); // temp string
(fast no gc)
v2 = Convert.ToInt32(sval.Substring(s + 1)); // idem
}
"Steven Blair" <st**********@btinternet.comha scritto nel messaggio
news:eh**************@TK2MSFTNGP04.phx.gbl...
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 ***

Oct 17 '06 #3
Hi Laura

why do you say 'no gc' in the line with Substring method. The Substring
method surely creates a new string-instance on the GC-Heap.
To avoid this string instances, one has also to avoid the Convert class and
the Parse method at all. It could be possible, to access the single chars
from the string and calculate the values directly.
But i'm not sure if this really matters. To know this one has to make
performance tests.

"Laura T" <la******@yahoo.comschrieb im Newsbeitrag
news:ek**************@TK2MSFTNGP02.phx.gbl...
Efficient depends on various parameters like speed, memory usage ecc.
I'd use something like this:

string sval = "44.55";
int v1, v2;

int s = sval.IndexOf('.'); // linear but small strings
if (s < 0) // Case no decimals
{
v1 = Convert.ToInt32(sval); // full integer
}
else
{
v1 = Convert.ToInt32(sval.Substring(0, s)); // temp
string (fast no gc)
v2 = Convert.ToInt32(sval.Substring(s + 1)); // idem
}
"Steven Blair" <st**********@btinternet.comha scritto nel messaggio
news:eh**************@TK2MSFTNGP04.phx.gbl...
>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 ***


Oct 17 '06 #4
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 ***
Oct 17 '06 #5

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

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
3
by: sandeep | last post by:
Hi i am new to this group and to c++ also though i have the knowledge of "c" and now want to learn c++ and data structure using c/c++ . so could nebody please suggest me some...
2
by: Vance M. Allen | last post by:
Greetings, I am establishing a database for the purpose of logging access to my secure webserver and am wanting to make the database as efficient as I can because it will be doing a lot of work...
15
by: Tor Erik Sønvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
2
by: SR | last post by:
Hi, I was wondering if there any good books or articles on efficient programming in C to achieve reductions in computation time(especially in parallel environments) etc. Pointers to any books or...
10
by: Teis Draiby | last post by:
In an application manipulating streaming video and 3D stuff I want to implement mutithreading to ensure a decent UI response time. Since my application is very speed critical I want to use the...
3
by: Brian Wotherspoon | last post by:
I have a table with data that is refreshed regularly but I still need to store the old data. I have created a seperate table with a foreign key to the table and the date on which it was replaced. ...
16
by: Dustan | last post by:
I have a program that uses up a lot of CPU and want to make it is efficient as possible with what I have to work with it. So which of the following would be more efficient, knowing that l is a list...
5
by: Alan Little | last post by:
I have affiliates submitting batches of anywhere from 10 to several hundred orders. Each order in the batch must include an order ID, originated by the affiliate, which must be unique across all...
25
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
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: 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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.