|
From: Robert H. <en...@no...> - 2005-06-09 16:12:20
|
On Thu, 2005-06-09 at 21:18 +0800, Hans-Bernhard Br=F6ker wrote:
> Robert Hart wrote:
>=20
> > strtod() is indeed significantly faster than sscanf.=20
>
> And I still don't see why that should be the case...
sscanf takes an arbitrary format string as an argument, which it must
parse every time it is called, it is also very generic so, I imagine,
ends up doing a lot of extra work to support this. If you wanted to you
could look through the source for libc and figure it out.
> > Can anybody come up with any cases where these two methods give
> > different results/error handling?
>=20
> Well, here's an ancient comment right taken directly from datafile.c:
>=20
> /* cannot trust strtod - eg strtod("-",&p) */
I've seen this comment, and tried it out. In my tests this isn't an
issue. The man page says:
If no conversion is performed, zero is returned and the value of n=
ptr
is stored in the location referenced by endptr.
So if(nptr!=3Dendptr) you know it failed.
Am I missing something? Are there other libc's that behave differently?
I have attached my test program. I can't find any input on my system
that gives different results between sscanf and strtod.
> > strtod is ANSI C, so should be
> > portable.
>=20
> So should sscanf().
Well comments in the code say "%n" doesn't work on OSK. I don't even
know what OSK is, but if strtod works where sscanf doesn't then that's a
win.
Rob
--=20
Robert Hart <en...@no...>
University of Nottingham
This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.
|