Fast Conversions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Fast Conversions

 
0
  #1
Sep 25th, 2008
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
  1. double Number1;
  2. char* Num = "8.12";
  3.  
  4. for(int i = 0; i < 2000000; i++)
  5. {
  6. Number1 = atof(Num);
  7. }
  8. MessageBox::Show("Finish");

std::string to double takes 35 sec
  1. double Number1;
  2. std::string Num = "8.12";
  3.  
  4. for(int i = 0; i < 2000000; i++)
  5. {
  6. stringstream v1(Num);
  7. v1 >> Number1;
  8. }
  9. MessageBox::Show("Finish");

double to std::string takes 51 sec
  1. double Number1 = 8.12;
  2. std::string Num;
  3.  
  4. for(int i = 0; i < 2000000; i++)
  5. {
  6. stringstream v1;
  7. v1 << Number1;
  8. Num = v1.str();
  9. }
  10. MessageBox::Show("Finish");
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,407
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Fast Conversions

 
0
  #2
Sep 25th, 2008
You test may not be entirely accurate, for example in this part of the code:
for (int i = 0; i < 2000000; i++) {			
   stringstream v1(Num);
   v1 >> Number1;
}
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:
  1. double Number1;
  2. std::string Num = "8.12";
  3. stringstream v1;
  4.  
  5. for (int i = 0; i < 2000000; i++) {
  6. v1 << Num;
  7. v1 >> Number1;
  8. v1.clear();
  9. }
Last edited by William Hemsworth; Sep 25th, 2008 at 1:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Fast Conversions

 
0
  #3
Sep 25th, 2008
Yes you are right. When changing that, the test went down from 35 to 20 seconds.
Great to think about these details !
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Fast Conversions

 
0
  #4
Sep 25th, 2008
I don't know about C++, but in C you can use the function fcvt .. which I think is faster than sprintf . See here
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Fast Conversions

 
0
  #5
Sep 25th, 2008
(deleted post)
Last edited by Jennifer84; Sep 25th, 2008 at 6:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Fast Conversions

 
0
  #6
Sep 26th, 2008
> 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:
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <string>
  6. #include <sstream>
  7. #include <math.h>
  8.  
  9. #pragma unmanaged // generate native code
  10. void double_to_string()
  11. {
  12. std::clock_t start = std::clock() ;
  13. for(int i = 0; i < 2000000; i++)
  14. {
  15. double Number1 = 8.12;
  16. static std::string Num;
  17. static std::stringstream v1;
  18. v1 << Number1;
  19. v1 >> Num;
  20. }
  21. std::clock_t end = std::clock() ;
  22. std::cout << "double_to_string: "
  23. << double( end-start ) / CLOCKS_PER_SEC << '\n' ;
  24. }
  25. #pragma managed
  26.  
  27. #pragma unmanaged // generate native code
  28. void string_to_double()
  29. {
  30. std::clock_t start = std::clock() ;
  31. for(int i = 0; i < 2000000; i++)
  32. {
  33. double Number1 ;
  34. static std::string Num = "8.12" ;
  35. static std::stringstream v1;
  36. v1 << Num;
  37. v1 >> Number1;
  38. }
  39. std::clock_t end = std::clock() ;
  40. std::cout << "string_to_double: "
  41. << double( end-start ) / CLOCKS_PER_SEC << '\n' ;
  42. }
  43. #pragma managed
  44.  
  45. #pragma unmanaged // generate native code
  46. void cstrng_to_double()
  47. {
  48. std::clock_t start = std::clock() ;
  49. for(int i = 0; i < 2000000; i++)
  50. {
  51. double Number1 ;
  52. const char* Num = "8.12";
  53. Number1 = std::atof(Num);
  54. }
  55. std::clock_t end = std::clock() ;
  56. std::cout << "cstrng_to_double: "
  57. << double( end-start ) / CLOCKS_PER_SEC << '\n' ;
  58. }
  59. #pragma managed
  60.  
  61. int main( array< System::String^ >^ args )
  62. {
  63. for( int i=0 ; i<4 ; ++i )
  64. {
  65. double_to_string() ;
  66. string_to_double() ;
  67. cstrng_to_double() ;
  68. std::cout << '\n' ;
  69. }
  70. }

on T2330 @ 1.6 GHz VC++ Express 2008 SP1 with Full Optimization (/Ox)

  1. double_to_string: 0.936
  2. string_to_double: 0.905
  3. cstrng_to_double: 1.357
  4.  
  5. double_to_string: 0.936
  6. string_to_double: 0.905
  7. cstrng_to_double: 1.342
  8.  
  9. double_to_string: 0.936
  10. string_to_double: 0.889
  11. cstrng_to_double: 1.326
  12.  
  13. double_to_string: 0.92
  14. string_to_double: 0.905
  15. 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Fast Conversions

 
0
  #7
Sep 26th, 2008
This is off topic, but I'm curious...

Why did you use msiL implementation with unmanaged code? Why not purely managed or purely unmanaged?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Fast Conversions

 
1
  #8
Sep 26th, 2008
only because the language the OP is using ( MessageBox::Show ) is C++/CLI (not ISO C++).
and purely managed will not give anywhere near native performance.
Last edited by vijayan121; Sep 26th, 2008 at 2:03 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Fast Conversions

 
0
  #9
Sep 26th, 2008
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:
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <string>
  6. #include <sstream>
  7. #include <math.h>
  8.  
  9. #pragma unmanaged
  10. void double_to_string()
  11. {
  12. std::clock_t start = std::clock() ;
  13. for(int i = 0; i < 200000; i++)
  14. {
  15. double Number1 = 8.12;
  16. static std::string Num ;
  17. std::ostringstream v1 ;
  18. v1 << Number1 ;
  19. Num = v1.str() ;
  20. }
  21. std::clock_t end = std::clock() ;
  22. std::cout << "double_to_string: "
  23. << double( end-start ) / CLOCKS_PER_SEC << '\n' ;
  24. }
  25. #pragma managed
  26.  
  27. #pragma unmanaged // generate native code
  28. void string_to_double()
  29. {
  30. std::clock_t start = std::clock() ;
  31. for(int i = 0; i < 2000000; i++)
  32. {
  33. double Number1 ;
  34. static std::string Num = "8.12" ;
  35. std::istringstream v1(Num);
  36. v1 >> Number1 ;
  37. }
  38. std::clock_t end = std::clock() ;
  39. std::cout << "string_to_double: "
  40. << double( end-start ) / CLOCKS_PER_SEC << '\n' ;
  41. }
  42. #pragma managed
  43.  
  44. #pragma unmanaged
  45. void cstrng_to_double()
  46. {
  47. std::clock_t start = std::clock() ;
  48. for(int i = 0; i < 2000000; i++)
  49. {
  50. double Number1 ;
  51. const char* Num = "8.12";
  52. Number1 = std::atof(Num);
  53. }
  54. std::clock_t end = std::clock() ;
  55. std::cout << "cstrng_to_double: "
  56. << double( end-start ) / CLOCKS_PER_SEC << '\n' ;
  57. }
  58. #pragma managed
  59.  
  60. int main( array< System::String^ >^ args )
  61. {
  62. for( int i=0 ; i<4 ; ++i )
  63. {
  64. double_to_string() ;
  65. string_to_double() ;
  66. cstrng_to_double() ;
  67. std::cout << '\n' ;
  68. }
  69. }

  1. double_to_string: 1.186
  2. string_to_double: 9.921
  3. cstrng_to_double: 1.311
  4.  
  5. double_to_string: 1.17
  6. string_to_double: 9.921
  7. cstrng_to_double: 1.295
  8.  
  9. double_to_string: 1.17
  10. string_to_double: 9.906
  11. cstrng_to_double: 1.295
  12.  
  13. double_to_string: 1.17
  14. string_to_double: 9.921
  15. cstrng_to_double: 1.295
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Fast Conversions

 
0
  #10
Sep 26th, 2008
Results of double_to_string() is somewhat off due to a missing zero ...

void double_to_string()
{
std::clock_t start = std::clock() ;
for(int i = 0; i < 2000000; i++)
{
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC