Skip to content

Commit

Permalink
add benchmarking method to quickseries
Browse files Browse the repository at this point in the history
  • Loading branch information
m-stclair committed Apr 4, 2024
1 parent 3e39193 commit cc5ddfe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ approx runtime:

## tips

* *NOTE: `quickseries.benchmark()` offers an easy way to test the precision and
efficiency of `quickseries.quickseries()`-generated approximations.*
* Narrowing `bounds` will tend to make the approximation more accurate within
those bounds. In the example above, setting `bounds` to `(-1, 1)` provides
~20x greater precision within the (-1, 1) interval (with the downside that
the resulting approximation will get pretty bad past about +/-pi/2).
the resulting approximation will get pretty bad past about +/-pi/2).
* `quickseries()` tends to be much more effective closer to 0. You may be
able to get more use out of it if you shift/squeeze your data towards 0.
* Increasing `order` will tend to make the approximation slower but more
precise. In the example above, increasing `order` to 14 provides ~20x
greater precision but makes the approximation ~20% slower.
Expand Down
4 changes: 2 additions & 2 deletions quickseries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from quickseries.approximate import quickseries
from quickseries.approximate import benchmark, quickseries

__version__ = "0.1.0"
__version__ = "0.1.1"
31 changes: 31 additions & 0 deletions quickseries/approximate.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,34 @@ def quickseries(
import numba
polyfunc = numba.njit(polyfunc)
return polyfunc


def benchmark(
func: Union[str, sp.Expr, sp.core.function.FunctionClass],
offset_resolution: int = 10000,
timeit_cycles: int = 10000,
testbounds = "equal",
**quickkwargs
):
lamb = lambdify(sp.sympify(func))
quick = quickseries(func, **quickkwargs)
if testbounds == "equal":
testbounds = quickkwargs.get("bounds", (-1, 1))
x_ax = np.linspace(*testbounds, offset_resolution)
# TODO: should probably permit specifying dtype for jitted
# functions -- both here and in primary quickseries().
approx_y, orig_y = quick(x_ax), lamb(x_ax)
approx_time = timeit.timeit(lambda: quick(x_ax), number=timeit_cycles)
orig_time = timeit.timeit(lambda: lamb(x_ax), number=timeit_cycles)
funcrange = min(orig_y), max(orig_y)
absdiff = max(abs(approx_y - orig_y))
orig_s = orig_time / timeit_cycles
approx_s = approx_time / timeit_cycles
return {
'absdiff': absdiff,
'reldiff': absdiff / (funcrange[1] - funcrange[0]),
'range': funcrange,
'orig_s': orig_s,
'approx_s': approx_s,
'timeratio': approx_s / orig_s
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="quickseries",
version="0.1.0",
version="0.1.1",
packages=find_packages(),
url="https://github.com/millionconcepts/quickseries.git",
author="Michael St. Clair",
Expand Down

0 comments on commit cc5ddfe

Please sign in to comment.