将字符串转换为数字 - 速度问题 [英] Converting strings to numbers - a question of speed

查看:95
本文介绍了将字符串转换为数字 - 速度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个极其cpu /数据密集的代码片段,使用以下函数:


void convertToDouble(const std ::字符串& in,double& out)

{

out = atof(in.c_str());

}


我真的想摆脱使用任何旧的C风格函数。

所以,我修改了上面的函数,使其遵循C ++惯例:


void convertToDouble(const std :: string& in,double& out)

{

std :: stringstream ss(in);

ss>> out;

}


但是,我的测试代码以前需要30秒才能运行,现在需要45s

(linux gcc 4.0) .2 with -03选项)。对于采用C ++约定的

来说,这是一个沉重的代价。所以,我的问题是......是否有一个

* *有效的方法将字符串转换为数字(例如double)而不需要

需要使用旧的C库?

I have an extremely cpu/data intensive piece of code that makes heavy
use of the following function:

void convertToDouble(const std::string& in, double& out)
{
out = atof(in.c_str());
}

I would really like to get away from using any old C-style functions.
So, I modified the above function to make it follow the C++ convention:

void convertToDouble(const std::string& in, double& out)
{
std::stringstream ss(in);
ss >> out;
}

However, my test code that previously took 30s to run, now takes 45s
(linux gcc 4.0.2 with -03 option). That''s a heavy price to pay for
adopting the C++ convention. So, my question is... Is there an
*efficient* way to convert a string to a number (e.g. double) without
requiring the use of old C libraries?

推荐答案

zexpe写道:
我有一个极其cpu /数据密集的代码片段重点
使用以下函数:

void convertToDouble(const std :: string& in,double& out)
{
out = atof(in.c_str) ());
}

我真的想摆脱使用任何旧的C风格函数。
所以,我修改了上面的函数使其遵循C ++惯例:

void convertToDouble(const std :: string& in,double& out)
{
std :: stringstream ss(in);
ss> >然而,我的测试代码以前需要30秒才能运行,现在需要45秒
(linux gcc 4.0.2 with -03选项)。采用C ++约定要付出很高的代价。所以,我的问题是......是否有一种有效的*方法将字符串转换为数字(例如double)而不需要使用旧的C库?
I have an extremely cpu/data intensive piece of code that makes heavy
use of the following function:

void convertToDouble(const std::string& in, double& out)
{
out = atof(in.c_str());
}

I would really like to get away from using any old C-style functions.
So, I modified the above function to make it follow the C++ convention:

void convertToDouble(const std::string& in, double& out)
{
std::stringstream ss(in);
ss >> out;
}

However, my test code that previously took 30s to run, now takes 45s
(linux gcc 4.0.2 with -03 option). That''s a heavy price to pay for
adopting the C++ convention. So, my question is... Is there an
*efficient* way to convert a string to a number (e.g. double) without
requiring the use of old C libraries?



C是合法的C ++(主要是)。如果C版本工作得更好,为什么不使用呢?

C库不老,它们是现代C ++的一部分。


john



C is legal C++ (mostly). If the C version works better, why not use it?
C libraries aren''t old, they''re part of modern C++.

john


zexpe写道:
我有一个极其cpu /数据密集的代码片段,它使得以下函数的使用非常重要:

void convertToDouble(const std :: string& in,double& out)
{
out = atof(in.c_str());
}

我真的想摆脱使用任何旧的C风格函数。
所以,我修改了上面的函数,使其遵循C ++惯例:

void convertToDouble(const std :: string& in,double& out)
{
std :: stringstream ss(in);
ss>>然而,我的测试代码以前需要30秒才能运行,现在需要45秒
(linux gcc 4.0.2 with -03选项)。采用C ++约定要付出很高的代价。所以,我的问题是......是否有一种有效的*方法将字符串转换为数字(例如double)而不需要使用旧的C库?
I have an extremely cpu/data intensive piece of code that makes heavy
use of the following function:

void convertToDouble(const std::string& in, double& out)
{
out = atof(in.c_str());
}

I would really like to get away from using any old C-style functions.
So, I modified the above function to make it follow the C++ convention:

void convertToDouble(const std::string& in, double& out)
{
std::stringstream ss(in);
ss >> out;
}

However, my test code that previously took 30s to run, now takes 45s
(linux gcc 4.0.2 with -03 option). That''s a heavy price to pay for
adopting the C++ convention. So, my question is... Is there an
*efficient* way to convert a string to a number (e.g. double) without
requiring the use of old C libraries?



嘿,你可以自己动手。 < evil grin>


但是说真的,使用atof()有什么异议?它是

语言的一部分有一个原因。 stringstream除了

之外还有很多其他的东西,所以你可以支付额外费用。


另外,你也许可以加快关于你的字符串流的一些东西

使用,具体取决于你的编译器正在做什么。例如,

如何使变量ss变为静态?您可以查看一些这样做的方法示例。也许ctor对strinstream的呼唤是

,其中有很多额外的时间。但是在你决定之前测量它。

你还需要更多地意识到在每次

调用时根据需要初始化它而不是依赖于ctor为你做好准备。

袜子



Hey, you could roll your own. <evil grin>

But seriously, what was the objection to using atof()? It''s part of the
language for a reason. stringstream does plenty of stuff other than
just what atof does, so you get to pay for the overhead.

Also, you might be able to speed up some stuff about your stringstream
use, depending on exactly what your compiler is doing. For example,
what about making your variable ss static? You might check a few
examples of ways of doing that. Maybe the ctor call for strinstream is
where a lot of the extra time is going. But measure it before you
decide.
You also need to be more aware of initializing it as required on each
call instead of depending on the ctor to prepare it for you.
Socks




John Harrison写道:

John Harrison wrote:
zexpe写道:




<剪切代码比较atof和stringstream>



<snip code comparing atof and stringstream>

但是,我的测试代码以前需要30秒才能运行,现在需要45s
(linux gcc 4.0.2 with -03选项)。采用C ++约定要付出很高的代价。所以,我的问题是......是否有一种有效的*方法将字符串转换为数字(例如double)而不需要使用旧的C库?
However, my test code that previously took 30s to run, now takes 45s
(linux gcc 4.0.2 with -03 option). That''s a heavy price to pay for
adopting the C++ convention. So, my question is... Is there an
*efficient* way to convert a string to a number (e.g. double) without
requiring the use of old C libraries?


C是合法的C ++(主要是)。如果C版本更好用,为什么不使用呢?
C库不老,它们是现代C ++的一部分。



C is legal C++ (mostly). If the C version works better, why not use it?
C libraries aren''t old, they''re part of modern C++.




I可能是错的,但是没有像atoi那样遭受同样的问题 -

即无法正确区分转换零

与错误案例。我相信strtod会是一个更好的C库

解决方案。


Gavin Deane



I may be wrong, but doesn''t atof suffer from the same problem as atoi -
namely that it''s impossible to distinguish correctly converting zero
from an error case. I believe strtod would be a better C library
solution.

Gavin Deane


这篇关于将字符串转换为数字 - 速度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆