473,508 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Processing std::strings into data

GGG
I have a situation where at tool is passing me a large array of
strings that I need to process in a particular type of data. Each item
in the array gets to me as a pair of std::strings, basically, one
string represents the data, one string represents the data type.
Here's a quick example of what it sort of looks like

string data[4] = { "0.0000002342", "12.00234", "42", "5" };
string types[4] = { "EF", "EF", "I", "I" }; //EF is 64 bit float,
I is 32 bit int

So element 0 of data matches with element 0 of types. i.e. types[0]
defines what type of data data[0] is.

This example is simple, however speed is going to be crucial as I need
to process a thousands of these in very short order. So I am looking
at ways make this work as fast as possible. I can't change the way the
data is being passed to me. I'm stuck with 2 std:string arrays. I'm
thinking a stringstream can work simple for this, but on my current
tests, my target machine can barley process this fast enough...

So I thought I could do something like this(using my dummy data above)

double doubleVal;
int intVal;

for( int i = 0; i < 4; i++) //just hardcode 4 as the size for this
example
{
stringstream tmpStream(data[i]);
if ( types[i] == "EF" )
{
tmpStream >> doubleVal;
//process the double here....
}
else if ( types[i] == "I" )
{
tmpStream >> intVal;
//process the integer here....
}
else
{
cerr << "kerblooy" << endl;
}
}

So basically I am wondering if there is something faster here than
using stringstream, or... is there a way I can declare stringstream
outside of the for loop so its destructor/constructor are not being
called constantly?

Now... I am already working on optimizing the "//process the value
here" part. I've played around with the code an seem to have found
that just this looping part is causing me a little more than have my
CPU usage time.

Any ideas would be cool.
-grant
Jul 22 '05 #1
3 1922
On 19 May 2004 16:54:11 -0700, gs*****@DigitalGlobe.com (GGG) wrote:
I have a situation where at tool is passing me a large array of
strings that I need to process in a particular type of data. Each item
in the array gets to me as a pair of std::strings, basically, one
string represents the data, one string represents the data type.
Here's a quick example of what it sort of looks like

string data[4] = { "0.0000002342", "12.00234", "42", "5" };
string types[4] = { "EF", "EF", "I", "I" }; //EF is 64 bit float,
I is 32 bit int

So element 0 of data matches with element 0 of types. i.e. types[0]
defines what type of data data[0] is.

This example is simple, however speed is going to be crucial as I need
to process a thousands of these in very short order. So I am looking
at ways make this work as fast as possible. I can't change the way the
data is being passed to me. I'm stuck with 2 std:string arrays. I'm
thinking a stringstream can work simple for this, but on my current
tests, my target machine can barley process this fast enough...

So I thought I could do something like this(using my dummy data above)

double doubleVal;
int intVal;

for( int i = 0; i < 4; i++) //just hardcode 4 as the size for this
example
{
stringstream tmpStream(data[i]);
if ( types[i] == "EF" )
{
tmpStream >> doubleVal;
//process the double here....
}
else if ( types[i] == "I" )
{
tmpStream >> intVal;
//process the integer here....
}
else
{
cerr << "kerblooy" << endl;
}
}

So basically I am wondering if there is something faster here than
using stringstream, or... is there a way I can declare stringstream
outside of the for loop so its destructor/constructor are not being
called constantly?

Now... I am already working on optimizing the "//process the value
here" part. I've played around with the code an seem to have found
that just this looping part is causing me a little more than have my
CPU usage time.

Any ideas would be cool.
I think you can do better, but precisely what approach you take may be
dependent on some details you haven't shown. For example, how many
different "types" are there? If the number is small, an if...else core to
your loop may be optimal. If the number is large, pre-loading a
map<string,int> with the types as keys and using the value (the int) as the
dispatch value in a switch may work better.

As for the string-to-value conversion, avoiding the constructor call for
the stringstream (it might be faster to use an istringstream, but I'm not
sure) would depend on finding a way to "reset" the istringstream with a new
string without actually re-initializing the whole thing, and I'm not quite
sure if there's a consensus on whether or not there's a way to even do that
(elegant or otherwise). But even if there is, don't forget about good ole'
sscanf, along with specialized library functions for the different types
(strtoul, atof, etc.). Using these would probably make a reasonable-sized
dent.

HTH,
-leor
-grant


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
gs*****@DigitalGlobe.com (GGG) wrote in message news:<98**************************@posting.google. com>...

[ ... this works, but it's too slow: ]
double doubleVal;
int intVal;

for( int i = 0; i < 4; i++) //just hardcode 4 as the size for this
example
{
stringstream tmpStream(data[i]);
if ( types[i] == "EF" )
{
tmpStream >> doubleVal;
//process the double here....
}
else if ( types[i] == "I" )
{
tmpStream >> intVal;
//process the integer here....
}
else
{
cerr << "kerblooy" << endl;
}
}


Here's a test program of a possible alternative:

#include <sstream>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>

std::string data[4] = { "0.0000002342", "12.00234", "42", "5" };
std::string types[4] = { "EF", "EF", "I", "I" }; //EF is 64 bit
float,

int ints = 0;
int doubles = 0;

void process(int i) {
ints ++;
}

void process(double d) {
doubles++;
}

void convert1() {
double doubleVal;
int intVal;

for( int i = 0; i < 4; i++) //just hardcode 4 as the size for
this example
{
if ( types[i] == "EF" )
{
doubleVal = std::atof(data[i].c_str());
process(doubleVal);
}
else if ( types[i] == "I" )
{
intVal = std::atoi(data[i].c_str());
process(intVal);
}
else
{
std::cerr << "kerblooy" << std::endl;
}
}
}

void convert2() {
double doubleVal;
int intVal;

for( int i = 0; i < 4; i++) //just hardcode 4 as the size for this
example
{
std::stringstream tmpStream(data[i]);
if ( types[i] == "EF" )
{
tmpStream >> doubleVal;
process(doubleVal);
}
else if ( types[i] == "I" )
{
tmpStream >> intVal;
process(intVal);
}
else
{
std::cerr << "kerblooy" << std::endl;
}
}
}

void show_time(std::string caption, void (*f)()) {
std::clock_t start = std::clock();
for (int i=0; i<100000; ++i)
f();
std::clock_t end = std::clock();

std::cerr << "Time " << caption << " " <<
double(end-start)/CLOCKS_PER_SEC << " seconds\n";
}

int main() {
show_time("using atoi/atof", convert1);
show_time("using stringstream", convert2);
std::cout << "int breaker: " << ints << std::endl;
std::cout << "double breaker: " << doubles << std::endl;
return 0;
}

Differences in speed obviously depend on the compiler and optimization
used, but with the compilers and libraries I have handy, the atoi/atof
version is always at least 3 times as fast, and sometimes up to 7
times as fast.

The calls to process and printing out the "breaker" numbers is to
break optimizers -- a good optimizer can eliminate the convesions if
it finds that the results are never used, so we ensure that they're
used (in a way that should have minimal effect on overall speed).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #3
GGG
>
I think you can do better, but precisely what approach you take may be
dependent on some details you haven't shown. For example, how many
different "types" are there? If the number is small, an if...else core to
your loop may be optimal. If the number is large, pre-loading a
map<string,int> with the types as keys and using the value (the int) as the
dispatch value in a switch may work better.

As for the string-to-value conversion, avoiding the constructor call for
the stringstream (it might be faster to use an istringstream, but I'm not
sure) would depend on finding a way to "reset" the istringstream with a new
string without actually re-initializing the whole thing, and I'm not quite
sure if there's a consensus on whether or not there's a way to even do that
(elegant or otherwise). But even if there is, don't forget about good ole'
sscanf, along with specialized library functions for the different types
(strtoul, atof, etc.). Using these would probably make a reasonable-sized
dent.

HTH,
-leor
-grant


Well, I can keep the if/else, there are a total of 5 types(much to my
happy releif... this particular software has been known to be...
bloaty...)

Wow... I made a test app to compare the speed of a few of these
methods... it basically was a 200000 cylce for loop that ran each of
the following methods( stringstream, istringstream, and scanf)

The average results(no compiler optimizations)
stringstream: 3.53 secs
istringstream: 3.15 secs
sscanf: 0.62 secs

average results WITH opt
stringstream: 3.37 secs
istringstream: 3.05 secs
sscanf: 0.62 secs
So it looks like optimizations help a tiny bit for the stringstream,
istringstream seems faster than stringstream, but scanf is the hands
down winner. Makes sense I know... I was a bit suprised however at the
that magnitude of difference.
Groovy
-grant
Jul 22 '05 #4

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

Similar topics

2
1749
by: William Cheung | last post by:
Hi all, I am a relative newbie to the PHP language but at present I am trying to write some code that would process text (in a string, not an array)by the explode() method. Basically, I want to...
1
1262
by: larry | last post by:
I'm trying to make a generic form processor, but I have several spots whre I need to include custom code (validation, UPDATEs, etc), now I could have four seperate files to include the necessary...
4
2427
by: D | last post by:
Hi folks, This may be pretty simple for you guys but it has me stumped. BTW I'm using Java 1.1, I know it's old, don't ask me why, I just have to. I have a long string in excess of 50k that I...
1
1662
by: Jacek Dziedzic | last post by:
i.e. is writing std::string s=""; unnecessary as std::string s; suffices? TIA, - J.
1
1847
by: Billy Cormic | last post by:
Hello, Some of the functions of my web application take a good amount of time to load. Is there a good way to show users that information is processing and will enventually be loaded? Like...
2
1262
by: Ney André de Mello Zunino | last post by:
Hello. Given the following try...catch construct: try { } catch (const string& msg) { }
3
5714
by: Lucas Tam | last post by:
Does anyone have easy to use sample code to build a "Please wait... processing data screen?" I'm interested in something like Expedia's search page Thanks. -- Lucas Tam...
1
1232
by: Vernon Wenberg III | last post by:
I have a script that processes a lot of data. Ideally it would be run from the CLI, however I want to run it from a browser, but I don't want the browser hanging on me forcing me to restart from...
1
1659
by: Anthony Smith | last post by:
I am allowing the use to cut and paste comma delimited data. How do I process the data one line at a time?
0
7233
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
7135
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
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7505
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
5650
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,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.