473,763 Members | 6,149 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.00000023 42", "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 1938
On 19 May 2004 16:54:11 -0700, gs*****@Digital Globe.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.00000023 42", "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*****@Digital Globe.com (GGG) wrote in message news:<98******* *************** ****@posting.go ogle.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.00000023 42", "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(doubleV al);
}
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::stringstre am tmpStream(data[i]);
if ( types[i] == "EF" )
{
tmpStream >> doubleVal;
process(doubleV al);
}
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("usin g atoi/atof", convert1);
show_time("usin g 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
1759
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 seperate different words out by using the explode(" ", $text); command, yet I find that this approach tends to die when I perform this on strings taken from texts with carriage returns in. Any suggestions to tackling this problem would be...
1
1273
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 code but I think that would be messy. Is there a way I can define a bunch of strings with stuff like: $validate="if ($list_data = 5) { $err = \"Data Sould be 5.\"; }";
4
2445
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 need to process in 32k chunks. The problem is all string processing in Java is done with integers. Is there an easy way to stream multiple 32k chunks of information from a string/array into a holding string/array?
1
1675
by: Jacek Dziedzic | last post by:
i.e. is writing std::string s=""; unnecessary as std::string s; suffices? TIA, - J.
1
1861
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 changing the cursor to an hour glass? Does anyone have any sample code? Thanks, Billy
2
1277
by: Ney André de Mello Zunino | last post by:
Hello. Given the following try...catch construct: try { } catch (const string& msg) { }
3
5723
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 (REMOVEnntp@rogers.com) Please delete "REMOVE" from the e-mail address when replying.
1
1249
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 scratch. My question is, how do I output data to the browser when something is processed when it is processed instead of having all the output come at once when the script is done? Is there a set amount of time where the page processing the...
1
1668
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
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.