Re: Converting double to widestring
- From: Ed <no@xxxxxxxxxxxx>
- Date: Wed, 26 Mar 2008 17:45:44 -0500
Alf P. Steinbach wrote:
* Ed:We have an application that converts a double to widestring (std::wstring). This happens thousands of time. We also need to provide how many decimal places to be used. The decimal point needs to be a period regardless of the locale of the machine. The function we call has signature like ConverDouble(std::wstring & str, double d, long digits)
What is the fastest way to do it?
First, be aware that you're likely heading for a case of evil premature optimization. Thousands of times is nothing, absolutely nothing, not even an eyeblink for the user. But given that you have measured and found that this is indeed a bottleneck that really really needs some optimization:
be aware that dynamic allocation for the std::wstring is likely to use orders of magnitude more time than the double->string conversion. So I'd suggest using a static buffer. For that, be aware of thread safety issues. An alternative, if this function is called in a tight loop in one place (or two, or three), is to only let the function resize the string and optimize at the caller site, where the same string variable should then be re-used repeatedly.
And given that, I'd prefer to suggest using the internal function that swprintf uses, namely _cfltcvt, which is just a macro that in turn references the first function pointer in array _cfltcvt_tab. However, when checking that out (that's how I found the names) the linker protested that it couldn't find this symbol. Not sure what needs to be linked, or how the heck it's found for printf itself.
If you find out how to use the _cfltcvt function, please report the details (of linking) here! :-)
Given that the _cfltcvt function is practically unavailable, I suggest plain wsprintf, which doesn't really add that much extra processing, only a little. A suitable format might be "%.*g", with precision as int argument. Note that your function signature has long precision (assuming "digits" means precision), and that that is most probably extreme overkill and Bad Design, but that to get rid of the dynamic allocation for wstring the signature may need to be adjusted anyway -- and I'd also suggest fixing the name, which says nothing whatsoever.
Then, again, measure.
Cheers, & hth.,
- Alf
Thanks for the reply. I removed some detail from my post for simplification. The actual function name is AppendDoubleToWideString. The function append the double to the string. The string is allocated in the caller and has more than enough space. I am currently using wsprintf but I will look at your suggestion too.
How about having a period for decimal notation even if the software is used in a, for example, Spanish machine?
Thanks again.
.
- Follow-Ups:
- Re: Converting double to widestring
- From: Nathan Mates
- Re: Converting double to widestring
- References:
- Converting double to widestring
- From: Ed
- Re: Converting double to widestring
- From: Alf P. Steinbach
- Converting double to widestring
- Prev by Date: Re: Converting double to widestring
- Next by Date: Re: Converting double to widestring
- Previous by thread: Re: Converting double to widestring
- Next by thread: Re: Converting double to widestring
- Index(es):
Relevant Pages
|