| | |
Fast Conversions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
I am looking for fast conversions. I found out that converting with atof is much more faster than using the stringstream like the below tests.
What I look for now is a fast way to convert from double to char*
I have a problem to find this conversion.
char* to double takes 3.1 sec
std::string to double takes 35 sec
double to std::string takes 51 sec
What I look for now is a fast way to convert from double to char*
I have a problem to find this conversion.
char* to double takes 3.1 sec
C++ Syntax (Toggle Plain Text)
double Number1; char* Num = "8.12"; for(int i = 0; i < 2000000; i++) { Number1 = atof(Num); } MessageBox::Show("Finish");
std::string to double takes 35 sec
C++ Syntax (Toggle Plain Text)
double Number1; std::string Num = "8.12"; for(int i = 0; i < 2000000; i++) { stringstream v1(Num); v1 >> Number1; } MessageBox::Show("Finish");
double to std::string takes 51 sec
C++ Syntax (Toggle Plain Text)
double Number1 = 8.12; std::string Num; for(int i = 0; i < 2000000; i++) { stringstream v1; v1 << Number1; Num = v1.str(); } MessageBox::Show("Finish");
•
•
Join Date: Mar 2008
Posts: 1,407
Reputation:
Solved Threads: 114
You test may not be entirely accurate, for example in this part of the code: Because here you are constructing stringstream 2000000 times, and destroying it 2000000 times, which obviously is going to add to the total amount of time, if you try making it a global or static variable, and just clear the stream every cycle, then hopefully this should speed it up. Like this:
for (int i = 0; i < 2000000; i++) {
stringstream v1(Num);
v1 >> Number1;
} C++ Syntax (Toggle Plain Text)
double Number1; std::string Num = "8.12"; stringstream v1; for (int i = 0; i < 2000000; i++) { v1 << Num; v1 >> Number1; v1.clear(); }
Last edited by William Hemsworth; Sep 25th, 2008 at 1:47 pm.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
I don't know about C++, but in C you can use the function fcvt .. which I think is faster than sprintf . See here
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> What I look for now is a fast way to convert from double to char*
> I have a problem to find this conversion.
a. compile C++ source to generate native code, not IL that is interpreted at run-time.
b. enable optimizations.
with these two, this is what i get:
on T2330 @ 1.6 GHz VC++ Express 2008 SP1 with Full Optimization (/Ox)
added later: just saw your thread 'fasted way to read a text file'. the two points above apply to that too.
> I have a problem to find this conversion.
a. compile C++ source to generate native code, not IL that is interpreted at run-time.
b. enable optimizations.
with these two, this is what i get:
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> #include <string> #include <sstream> #include <math.h> #pragma unmanaged // generate native code void double_to_string() { std::clock_t start = std::clock() ; for(int i = 0; i < 2000000; i++) { double Number1 = 8.12; static std::string Num; static std::stringstream v1; v1 << Number1; v1 >> Num; } std::clock_t end = std::clock() ; std::cout << "double_to_string: " << double( end-start ) / CLOCKS_PER_SEC << '\n' ; } #pragma managed #pragma unmanaged // generate native code void string_to_double() { std::clock_t start = std::clock() ; for(int i = 0; i < 2000000; i++) { double Number1 ; static std::string Num = "8.12" ; static std::stringstream v1; v1 << Num; v1 >> Number1; } std::clock_t end = std::clock() ; std::cout << "string_to_double: " << double( end-start ) / CLOCKS_PER_SEC << '\n' ; } #pragma managed #pragma unmanaged // generate native code void cstrng_to_double() { std::clock_t start = std::clock() ; for(int i = 0; i < 2000000; i++) { double Number1 ; const char* Num = "8.12"; Number1 = std::atof(Num); } std::clock_t end = std::clock() ; std::cout << "cstrng_to_double: " << double( end-start ) / CLOCKS_PER_SEC << '\n' ; } #pragma managed int main( array< System::String^ >^ args ) { for( int i=0 ; i<4 ; ++i ) { double_to_string() ; string_to_double() ; cstrng_to_double() ; std::cout << '\n' ; } }
on T2330 @ 1.6 GHz VC++ Express 2008 SP1 with Full Optimization (/Ox)
C++ Syntax (Toggle Plain Text)
double_to_string: 0.936 string_to_double: 0.905 cstrng_to_double: 1.357 double_to_string: 0.936 string_to_double: 0.905 cstrng_to_double: 1.342 double_to_string: 0.936 string_to_double: 0.889 cstrng_to_double: 1.326 double_to_string: 0.92 string_to_double: 0.905 cstrng_to_double: 1.342
added later: just saw your thread 'fasted way to read a text file'. the two points above apply to that too.
Last edited by vijayan121; Sep 26th, 2008 at 2:06 am.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
writing a convert function (with construction and destruction of a stringstream each time it is called) would give more maintainable code. even in this case, the performance seems to be acceptable:
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> #include <string> #include <sstream> #include <math.h> #pragma unmanaged void double_to_string() { std::clock_t start = std::clock() ; for(int i = 0; i < 200000; i++) { double Number1 = 8.12; static std::string Num ; std::ostringstream v1 ; v1 << Number1 ; Num = v1.str() ; } std::clock_t end = std::clock() ; std::cout << "double_to_string: " << double( end-start ) / CLOCKS_PER_SEC << '\n' ; } #pragma managed #pragma unmanaged // generate native code void string_to_double() { std::clock_t start = std::clock() ; for(int i = 0; i < 2000000; i++) { double Number1 ; static std::string Num = "8.12" ; std::istringstream v1(Num); v1 >> Number1 ; } std::clock_t end = std::clock() ; std::cout << "string_to_double: " << double( end-start ) / CLOCKS_PER_SEC << '\n' ; } #pragma managed #pragma unmanaged void cstrng_to_double() { std::clock_t start = std::clock() ; for(int i = 0; i < 2000000; i++) { double Number1 ; const char* Num = "8.12"; Number1 = std::atof(Num); } std::clock_t end = std::clock() ; std::cout << "cstrng_to_double: " << double( end-start ) / CLOCKS_PER_SEC << '\n' ; } #pragma managed int main( array< System::String^ >^ args ) { for( int i=0 ; i<4 ; ++i ) { double_to_string() ; string_to_double() ; cstrng_to_double() ; std::cout << '\n' ; } }
C++ Syntax (Toggle Plain Text)
double_to_string: 1.186 string_to_double: 9.921 cstrng_to_double: 1.311 double_to_string: 1.17 string_to_double: 9.921 cstrng_to_double: 1.295 double_to_string: 1.17 string_to_double: 9.906 cstrng_to_double: 1.295 double_to_string: 1.17 string_to_double: 9.921 cstrng_to_double: 1.295
![]() |
Similar Threads
- Search Engine Manager needed (Internet Marketing Job Offers)
- code optimization ... (C++)
Other Threads in the C++ Forum
- Previous Thread: Need urgent help
- Next Thread: Variables changing
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






