1

Just as the title, how do I convert double to string in scientific notation faster than sprintf in c++?

I have lots of (about 1e10) double numbers, and have to convert all to string in this format: ±*.*********E±***, which has 10 significance digits.

But sprintf and stringstream are too slow, Is there any faster way?

Thanks.

user1024
  • 982
  • 4
  • 13
  • 26
  • Have you tried to compare speed if strintf with speed of sstream with output manipulators? I suppose sprintf is faster :-) – VolAnd Feb 01 '15 at 07:18
  • of course `sprintf` is faster than `stringstream`, but i want faster than `sprintf`. – user1024 Feb 01 '15 at 07:27
  • Are you sure that sprintf is too slow? If you have doubles, what do you need strings for? Are you sure that it's not the operation you need the string for that is so slow? – nvoigt Feb 01 '15 at 07:43
  • Yes i am sure after profiling in VS2013. – user1024 Feb 01 '15 at 07:49
  • Does order matter? What are you outputting these 10 billion strings to, a file on a spinning disk? Have you determined that formatting is your bottleneck, and if so how did you do this, and what are the profiling resuts? – Yakk - Adam Nevraumont Feb 01 '15 at 08:02
  • 1
    Sprintf is very, very fast. It is tuned for speed by generations of programmers. The only way you can get it any faster is to get yourself the source code of sprintf and throw away everything that you don't need. Instead of parsing the format it should use your hardcoded values for precision and width. – n. m. could be an AI Feb 01 '15 at 08:28
  • Though I too wonder why you would need 10 billion strings for. Are you going to import them in a spreadsheet or what? – n. m. could be an AI Feb 01 '15 at 08:31
  • Well, it's a external sorting task, sort 10 billion doubles and store them in this format, after frofiling, `sprintf` took 20% time, so i want to improve it. – user1024 Feb 01 '15 at 10:15
  • @n.m. well, that's what i want, but i don't know how to implement my own `sprintf`, could you help me? – user1024 Feb 08 '15 at 04:55
  • As I said, you need to read source code for an existing implementation of `sprintf`, understand it, and modify it to suit your needs. – n. m. could be an AI Feb 08 '15 at 07:55
  • @n.m. But i couldn't find any existing implementation of `sprintf`. After stepping into the code, i got `_cfltcvt_l` function, but there is not `_cfltcvt_l` source code in Windows; in Linux, i got `__mingw_vsprintf`, the same, no source code. – user1024 Feb 10 '15 at 06:17
  • "couldn't find any existing implementation of sprintf" glibc is open source. http://www.gnu.org/software/libc/download.html – n. m. could be an AI Feb 10 '15 at 06:47

2 Answers2

1

Using boost is a way out

std::string str = boost::lexical_cast<std::string>(dbl);

The normal approach:

std::ostringstream strs;
strs << dbl;
std::string str = strs.str();

You should use sprintf() as it is the fastest.

Run Time of different functions @ http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html

References: http://www.codeproject.com/Questions/166322/converting-number-from-long-double-to-string

Need more help? Let me know

m0bi5
  • 8,900
  • 7
  • 33
  • 44
  • Is `boost` faster than `sprintf`? And I want my string in this format: ±*.*********E±***, which has 10 significance digits, how can i do? – user1024 Feb 01 '15 at 07:30
  • there is a nice article on performance here http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html , sprintf is faster – m0bi5 Feb 01 '15 at 07:36
  • So, Is there any way faster than `sprintf`? – user1024 Feb 01 '15 at 07:45
  • Your link said `fmt::Writer` is fastest when converting int to string, but i want to convert double to string in scientific notation, is that the same? – user1024 Feb 01 '15 at 07:56
0

The conversion you ask for is not a simple operation. You can have a look at How to implement " char * ftoa(float num) " without sprintf() library function in C, C++ and JAVA? to be sure.

I'm not sure that you will find an implementation that will be quicker that sprintf, because it is in standard C library for a time and can already have been optimised.

The best you can do is :

  • get the source from a standard C library
  • analyse how the conversion is done
  • compare with the links found in the question cited above
  • benchmark different implementations

I understand it is more a hint than a full annswer, but it is both too long for a comment and the best I can do

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • But i couldn't find any existing implementation of sprintf. After stepping into the code, i got `_cfltcvt_l` function, but there is not `_cfltcvt_l` source code in Windows; in Linux, i got `__mingw_vsprintf`, the same, no source code. – user1024 Feb 10 '15 at 06:18