P
puzzlecracker
does anyone know how to implement this function efficiently?
does anyone know how to implement this function efficiently?
puzzlecracker said:does anyone know how to implement this function efficiently?
Re: implement atoi
does anyone know how to implement this function efficiently?
Allan said:does anyone know how to implement this function efficiently?
Here is my fast implementation of atoi. Please bear in mind that this has
no error checking, and since I have total control of the inputs to this
program - it works for me. If you need error checking then you would need
to add it. Also, this is a #define for optimal speed, it might not give
best filesize but my program was reading thousands, if not millions of
numbers from ascii so this was the best way (I also have an equivalent
strtod).
[code snipped]
Eric Sosman said:Allan said:does anyone know how to implement this function efficiently?
Here is my fast implementation of atoi. Please bear in mind that this
has
no error checking, and since I have total control of the inputs to this
program - it works for me. If you need error checking then you would
need
to add it. Also, this is a #define for optimal speed, it might not give
best filesize but my program was reading thousands, if not millions of
numbers from ascii so this was the best way (I also have an equivalent
strtod).
[code snipped]
<irony>
Good move. I just ran a quick and dirty timing comparison
of your macro with my system's atoi() function. On the rather
old and slow machine that happens to sit in front of me, one
million executions of your macro ran 0.093 seconds faster than
one million executions of atoi(). If it took you, say, five
minutes to code the macro, you'll break even as soon as you've
converted 3.2 billion numbers -- and after that, it's all gain.
But that comparison isn't really fair: after all, your
macro also reports the length of the converted digit string,
which is more than atoi() does. The comparison really should
be against strtol(), so I tried that one as well. A million
executions of your macro clocked in at a whopping 0.414 seconds
faster than a million strtol() calls, so the break-even point
is really more like 0.7 billion conversions. Macroz rulez!
</irony>
Of course, timings on your machine will differ from those
on mine. Still, I strongly doubt that the speed of atoi() will
make any noticeable difference in the performance of what sounds
like an I/O-bound program. Were you able to measure an
improvement?
puzzlecracker said:does anyone know how to implement this function efficiently?
Allan said:[...]
To be honest I wrote this macro as I was heavily using atoi and strtod. At
the time, I believed that the atoi was being slow so thats why I chose it,
but there wasnt much of a speed gain. However when I use me strtod macro,
it is much much faster. Here is the code and you can have a speed test,
would be interesting to see if the results are similar to what I achieved:
[code snipped; see up-thread]
Eric said:Allan said:[...]
To be honest I wrote this macro as I was heavily using atoi and
strtod. At the time, I believed that the atoi was being slow so
thats why I chose it, but there wasnt much of a speed gain.
However when I use me strtod macro, it is much much faster. Here
is the code and you can have a speed test, would be interesting
to see if the results are similar to what I achieved:
[code snipped; see up-thread]
This may well be faster than strtod(), at least on some
machines. You'd need to measure to find out. The speed can
probably be improved -- that call to pow() can probably be
done away with, and several micro-optimizations suggest
themselves. Again, you'd need to measure to see whether any
such changes actually represent improvements.
One thing worth noting is that the macro is a little
sloppy about accuracy -- more than "a little" sloppy with
inputs containing a lot of fraction digits! This may be good
enough for the application at hand, but I wouldn't recommend
this macro for use as a general-purpose strtod() replacement.
(Interesting that it falls back on strtod() in some cases;
given the way pow() is being used one would think those cases
would be easy to handle in line.)
Stepping back a bit, I'd suggest that anyone interested in
re-implementing Standard library functions (either "as specified"
or in streamlined reduced-capability forms) should study P.J.
Plauger's "The Standard C Library" for an exposition of the many
non-obvious concerns that can arise.
CBFalconer said:Rather than reimplement standard library stuff, I have some
routines here designed for numeric input from text streams. They
are intended to avoid any fuss with buffers, be capable of skipping
to numeric input, possibly over blank lines, and to universally
report overflows. It is always possible to discover what character
terminated the numeric field because it is always pushed back.
This also means there is no need to wonder whether a line remainder
needs flushing - it always does.
Eric said:>
(Some systems provide a non-Standard hypot() function
to perform this calculation with better speed and accuracy
than the classical Pythagorean formulation can yield.)
Eric Sosman said:Allan said:[...]
To be honest I wrote this macro as I was heavily using atoi and strtod.
At
the time, I believed that the atoi was being slow so thats why I chose
it,
but there wasnt much of a speed gain. However when I use me strtod
macro,
it is much much faster. Here is the code and you can have a speed test,
would be interesting to see if the results are similar to what I
achieved:
[code snipped; see up-thread]
This may well be faster than strtod(), at least on some
machines. You'd need to measure to find out. The speed can
probably be improved -- that call to pow() can probably be
done away with, and several micro-optimizations suggest
themselves. Again, you'd need to measure to see whether any
such changes actually represent improvements.
One thing worth noting is that the macro is a little
sloppy about accuracy -- more than "a little" sloppy with
inputs containing a lot of fraction digits! This may be good
enough for the application at hand, but I wouldn't recommend
this macro for use as a general-purpose strtod() replacement.
(Interesting that it falls back on strtod() in some cases;
given the way pow() is being used one would think those cases
would be easy to handle in line.)
Stepping back a bit, I'd suggest that anyone interested in
re-implementing Standard library functions (either "as specified"
or in streamlined reduced-capability forms) should study P.J.
Plauger's "The Standard C Library" for an exposition of the many
non-obvious concerns that can arise.
Assailed by sudden doubt, I checked the Standard: lo!
C99 added the hypot() function, which was absent from C90.
The adjective "non-Standard" is thus version-dependent.
Chris said:And thus introduced another incompatibility between the versions
(there was no way to predict that a hypot() function might be
added to the standard header math.h). The math functions don't
even have their own namespace like the str* and mem* ones do.
I'm coming to the conclusion that the only way to be safe is to
include at least one uppercase letter in every function and
variable name, otherwise it could conflict with some future
'standard' function or keyword (perfectly valid C89 programs
using a variable called 'restrict' for instance)...
Keywords (reserved words) are one thing, function names another.
Apart from a few cases it is usually possible to replace a system
function with your own by simply linking it before the library
search.
This is definitely not standard compliant, but is
practicable in most systems known to me. Areas such as the math
library could easily be forced to adhere to this capability in a
future standards revision. There could simply be a list of header
names, whose functions are replaceable. Or the individual
functions could have another characteristic, "replaceable". Some
functions would need to be grouped, so that either none or all of
the group must be replaced.
You are correct about the pow() function - I actually have changed that in a
more recent version of my program. I achieve a massive speed increase using
this. I tested 7 million numbers and with the library strtod it takes
approximately 10.71 secs wheras with my macro it takes only 0.241 seconds to
read through the same numbers, a speed increase of nearly 45 times!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.