Comments (6)
I would expect them to perform equivalently. I suspect the reason you are measuring fastnumbers.float
to be slower is that you are incurring the python lookup overhead with the dot operator. If you import as from fastnumbers import float as fnfloat
then use fnfloat
everywhere I suspect you should see the timings be about the same.
Why do I only expect them to be the same and not fastnumbers.float
be faster?
For floats with as many digits as you have the fast algorithm does not kick in - it only works when there is less precision. This is hinted at in the How is fastnumbers
so fast? section of the readme (emphasis below added to draw the eye).
CPython goes to great lengths to ensure that your string input is converted to a number correctly (you can prove this to yourself by examining the source code for integer conversions and for float conversions), but this extra effort is only needed for very large integers or for floats with many digits or large exponents. For integers, if the result could fit into a C long then a naive algorithm of < 10 lines of C code is sufficient. For floats, if the number does not require high precision or does not have a large exponent (such as "-123.45e6") then a short naive algorithm is also possible.
These naive algorithms are quite fast, but the performance improvement comes at the expense of being unsafe (no protection against overflow or round-off errors). fastnumbers uses a heuristic to determine if the input can be safely converted with the much faster naive algorithm. These heuristics are extremely conservative - if there is any chance that the naive result would not give exactly the same result as the built-in functions then it will fall back on CPython's conversion function. For this reason, fastnumbers is aways at least as fast as CPython's built-in float and int functions, and oftentimes is significantly faster because most real-world numbers pass the heuristic.
In your case, the heuristic is not passed.
I tried to highlight this in the timing tests where I showed the timing of "small" floats (e.g. low precision and low exponent) and "large" floats, and that the "large" floats are no faster than standard python.
from fastnumbers.
@c0ver Did changing the import methodology give better results?
from fastnumbers.
With the same numbers:
In [1]: from fastnumbers import float as fnfloat
In [8]: start_time = time.time()
...: for numb in numbs:
...: a = float(numb)
...: print(time.time() - start_time)
...:
0.36107563972473145
In [9]: start_time = time.time()
...: for numb in numbs:
...: a = fnfloat(numb)
...: print(time.time() - start_time)
...:
0.4131138324737549
Also, are you saying fnfloat('3.1') should be faster than float('3.1')?
In [17]: %timeit fnfloat('3.1')
The slowest run took 43.07 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 159 ns per loop
In [18]: %timeit float('3.1')
The slowest run took 23.25 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 86.8 ns per loop```
from fastnumbers.
On my OS (arch linux) and compiler (gcc) and hardware (Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz), I observe the following:
fastnumbers
is between 1.5x to 2.0x as fast as the builtinfloat
for Python 3.6fastnumbers
is only marginally faster (close to within the noise) than the builtinfloat
on Python 3.7- The speedup for integers is about the same for Python 3.6 and Python 3.7
So, I conclude that the Python devs have made quite a bit of improvements in their conversion methodology for floating point numbers so that fastnumbers.float
is less meaningful
Thank you for pointing this out to me. I had not yet done timing tests on Python 3.7. I will update the documentation to make clear that users should evaluate for themselves on their own machines if using fastnumbers
buys them anything (assuming they only want to use the builtin replacement functions... there is still a huge win if you want to use the functions with error-handling incorporated).
from fastnumbers.
Alright. Thanks for double checking my numbers.
from fastnumbers.
I'm going to keep this open till I update the documentation.
from fastnumbers.
Related Issues (20)
- Integrate with numpy and pandas HOT 9
- Unit test numeric issues on 32bit arm CPU HOT 3
- Make most options keyword-only
- Rename "key" option to "on_fail"
- [BUG] FastNumbers can crash with a SystemError due to returning NULL without setting an exception HOT 5
- Fastest way to check is and object is int or float in one pass HOT 14
- Proposal: change behavior of isfloat with respect to treatment of float("nan") HOT 19
- Proposal: change behavior of isfloat function with respect to treatment of strings containing integers HOT 3
- Proposal: Do not raise an exception on None HOT 5
- python3.9 compatibility HOT 5
- Re-write using C++ and pybind11
- Add support to release Linux aarch64 wheels HOT 1
- Broken 3.2.0 installation
- Missing -lm breaks build on armv7hl
- Error: <built-in function isint/isfloat> returned NULL without setting an error HOT 2
- Use fast C++ methods like std::from_chars or fast_float HOT 1
- Improve performance with METH_FASTCALL
- Add support for operating on iterables
- Add numpy support? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastnumbers.