943,551 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1077
  • C++ RSS
Sep 25th, 2008
0

Fast Conversions

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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");
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Sep 25th, 2008
0

Re: Fast Conversions

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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Sep 25th, 2008
0

Re: Fast Conversions

Yes you are right. When changing that, the test went down from 35 to 20 seconds.
Great to think about these details !
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Sep 25th, 2008
0

Re: Fast Conversions

I don't know about C++, but in C you can use the function fcvt .. which I think is faster than sprintf . See here
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 25th, 2008
0

Re: Fast Conversions

(deleted post)
Last edited by Jennifer84; Sep 25th, 2008 at 6:20 pm.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Sep 26th, 2008
0

Re: Fast Conversions

> 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:
C++ Syntax (Toggle Plain Text)
  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)

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 26th, 2008
0

Re: Fast Conversions

This is off topic, but I'm curious...

Why did you use msiL implementation with unmanaged code? Why not purely managed or purely unmanaged?
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Sep 26th, 2008
1

Re: Fast Conversions

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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 26th, 2008
0

Re: Fast Conversions

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)
  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. }

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 26th, 2008
0

Re: Fast Conversions

Results of double_to_string() is somewhat off due to a missing zero ...

Quote ...
void double_to_string()
{
std::clock_t start = std::clock() ;
for(int i = 0; i < 2000000; i++)
{
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need urgent help
Next Thread in C++ Forum Timeline: Variables changing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC