|

楼主 |
发表于 2009-11-17 11:49:00
|
显示全部楼层
Re: Re:自己写的字符串到double函数,没想到比库函数还快
骆驼祥子: Re:自己写的字符串到double函数,没想到比库函数还快。大家帮忙测试哈!
http://blog.csdn.net/aheroofeast/archive/2008/04/12/2284413.aspx
look一下我的,没有算法,速度也不...
我先前就一直再用,测了一下比C的atof()还慢,所以才决定自己写一个的。
//字符串转换为数值
template <typename T>
T string_value_A( const std::string& str )
{
T value;
std::stringstream( str.c_str() ) >> value;
return value;
}
int main()
{
//高精度定时器
high_timer timer;
if(!timer.initialize())
{
wcout<<L"初始化定时器失败!"<<endl;
return 0;
}
double t0=0, t1=0;
t0=timer.get_time();
double v1 = string_value_A<double>("0.15");
t1=timer.get_time();
cout<<"结果"<<v1<<ends;
cout<<"旧算法耗时:"<<(t1-t0)*1000<<"ms"<<endl<<endl;
t0=timer.get_time();
double v2 = str_double_A("0.15");
t1=timer.get_time();
cout<<"结果"<<v2<<ends;
cout<<"新算法耗时:"<<(t1-t0)*1000<<"ms"<<endl<<endl;
return 0;
}
结果如下: |
|