[英]Convert double to string in scientific notation faster than sprintf in c++
就像標題一樣,如何用科學計數法將double轉換為字符串比使用c ++的sprintf
更快?
我有很多(大約1e10)個雙數,並且必須將所有格式都轉換為以下格式的字符串:±*。********* E±***,其中有10位有效數字。
但是sprintf
和stringstream
太慢了,有沒有更快的方法?
謝謝。
使用升壓是一種出路
std::string str = boost::lexical_cast<std::string>(dbl);
正常方法:
std::ostringstream strs;
strs << dbl;
std::string str = strs.str();
您應該使用 sprintf()
因為它是最快的。
不同功能的運行時間@ http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html
參考: http : //www.codeproject.com/Questions/166322/converting-number-from-long-double-to-string
需要更多幫助嗎? 讓我知道
您要求的轉換不是簡單的操作。 您可以看看在C,C ++和JAVA中如何在沒有sprintf()庫函數的情況下實現“ char * ftoa(float num)”? 為了確定。
我不確定您會找到比sprintf更快的實現,因為它已經存在於標准C庫中一段時間了,並且已經進行了優化。
您能做的最好的事情是:
我了解這比完整的回答更能暗示問題,但是對於評論和我所能做到的一切都太長了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.