By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

atoi: stringstream or old C sprintf function

P: n/a
Sorry about the newbie question. What's the best way to convert
numbers to strings in C++. The following works, but is it better than
using the sprintf() "old C" way of converting?
#include <iostream>
#include <string>
#include <sstream>
#include <list>

using namespace std;
int main(void)
{
list<string> ml;
string s;
stringstream ss;

for(int i=0; i < 3000; ++i)
{
ss << "sample text " << i ;
ml.push_front( ss.str() );
ss.str("");
}

cout << ml.front() << endl;
}
What about the fact that the stringstream has to be cleared
ss.str("")...does that incur additional overhead? Maybe there is a
better way?
Regards,
Mike Chirico
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> Sorry about the newbie question. What's the best way to convert
numbers to strings in C++. The following works, but is it better than
using the sprintf() "old C" way of converting?
#include <iostream>
#include <string>
#include <sstream>
#include <list>

using namespace std;
int main(void)
{
list<string> ml;
string s;
stringstream ss;

for(int i=0; i < 3000; ++i)
{
ss << "sample text " << i ;
ml.push_front( ss.str() );
ss.str("");
}

cout << ml.front() << endl;
}
What about the fact that the stringstream has to be cleared
ss.str("")...does that incur additional overhead? Maybe there is a
better way?


Is your program too slow? If yes, is it caused by stringstream? If the
answer is no to either of these questions, then why worry about the
overhead? Unless the stringstream introduces an unacceptable perfomance
problem in your program, I'd say stick with what you have got; it looks
pretty good to me!

Since ss is only used inside the loop, you might consider moving the
"stringstream ss;" line inside loop and remove the "ss.str("");" line.
It is a little bit cleaner, though it is probably also slightly slower
(but I doubt you'd be able to measure the difference).

The std::stringstream solution is not necessarilly the fastest solution,
but it is clean and safe unlike the C way of converting. The overhead of
ss.str("") is probably very small compared to the cost of doing
ml.push_front(ss.str());.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #2

P: n/a
Mike Chirico wrote:
What's the best way to convert numbers to strings in C++. The
following works, but is it better than using the sprintf() "old
C" way of converting?


http://www.parashift.com/c++-faq-lit....html#faq-38.1
http://www.parashift.com/c++-faq-lite/input-output.html

Jul 22 '05 #3

P: 1
Yes stringstreams are still terribly slow (at least on gcc).
Did a little benchmark comparing strtod and ostringstream.

Again if you can live with the overhead, it is the cleanest way to do string conversions (I use streams a lot but on some places going back to oldschool C-style atoi, strtod etc. is necessary)

This is copied from my page stringstream benchtest


Here follows a simple example (okay did not try to optimize anything here,
just used sstream in a classroom type example and compared it to the c-style
sprintf and strtod functions).

What we notice is :
1. The cstreams implementation is almost 5 times faster than cppstreams
2. The cppstreams gives an extra double which differs from original
after conversion to string and back. The differences between value and repVal are normal/understandable but it's not logical why the cpp stringstreams have one more difference between value/repVal than when using srtod+sprintf...

Here are my files and a testrun:


wschrep@pascal:~/cpp-work/streambench.cpp> ls

cppstreams.cpp cppstreams.out cstreams.cpp cstreams.out Makefile

wschrep@pascal:~/cpp-work/streambench.cpp> cat cppstreams.cpp

#include <iostream>

#include <sstream>



using namespace std;



int main(){

double value = 1.0;

double repVal= 0;

for( int i=0;i<100000;i++){

value = (value/2)+1;



//double to string

ostringstream os;

os << value << endl;

string strVal = os.str();



//string to double

istringstream is( strVal );

is >> repVal;

if( repVal != value ){

cout << "repVal = "<<repVal<<" != value =" << value <<endl;

}

}

cout << "done!"<<endl;

return 0;

}



wschrep@pascal:~/cpp-work/streambench.cpp> cat cstreams.cpp

#include <iostream>

#include <sstream>

using namespace std;

#include <stdio.h>

#include <stdlib.h>



int main(){

double value = 1.0;

double repVal= 0;



for( int i=0;i<100000;i++){

value = (value/2)+1;



//double to string

char cp[255];

sprintf( cp, "%f", value );

string strVal( cp );



//string to double

char * pEnd;

repVal = strtod( strVal.c_str(), &pEnd );

if( repVal != value ){

cout << "repVal = "<<repVal<<" != value =" << value <<endl;

}

}

cout << "done!"<<endl;

return 0;

}



wschrep@pascal:~/cpp-work/streambench.cpp> cat Makefile

INCLUDE = -I.

CXX = g++

CXXFLAGS = -O2 -Wall $(INCLUDE)



all: cppstreams cstreams



cppstreams: cppstreams.o

$(CXX) $(CXXFLAGS) -o cppstreams cppstreams.o



cstreams: cstreams.o

$(CXX) $(CXXFLAGS) -o cstreams cstreams.o

clean:

@rm -vf *.o *~ DEADJOE cppstreams cstreams



wschrep@pascal:~/cpp-work/streambench.cpp> make

g++ -O2 -Wall -I. -c -o cppstreams.o cppstreams.cpp

g++ -O2 -Wall -I. -o cppstreams cppstreams.o

g++ -O2 -Wall -I. -c -o cstreams.o cstreams.cpp

g++ -O2 -Wall -I. -o cstreams cstreams.o

wschrep@pascal:~/cpp-work/streambench.cpp> time ./cppstreams > cppstreams.out



real 0m1.558s

user 0m1.557s

sys 0m0.001s

wschrep@pascal:~/cpp-work/streambench.cpp> time ./cstreams > cstreams.out



real 0m0.334s

user 0m0.331s

sys 0m0.002s

wschrep@pascal:~/cpp-work/streambench.cpp> diff cstreams.out cppstreams.out

0a1

> repVal = 1.98438 != value =1.98438



So wassup here dudes? Don't have time to find a mailing list to post this so I'm just gonna put it here in my DIY blog

Incidently I was benching these 2 methods of conversion for my matrix class which will have to do such conversions from string to double and other types when parsing scripts and interpreting them...
For now I'm probably going to write a little wrapper class around the c-style conversion I guess... since it gives such a speed improvement
Jul 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.