Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

~2x speedup using numba decorator in one single place #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ or:

The cli project contains valid console script entrypoints for potrace. If you install the command-line package it will add `potracer` to your console scripts. Note the `-r` suffix so that it does not interfere with potrace that may be otherwise installed.

### Numba (optional, experimental)
[numba](https://github.com/numba/numba), an open source jit compiler for python & numpy, has been shown to speed up python potrace considerably (for complex images approx. a factor of 2).
While numba supports most [platforms](https://numba.pydata.org/numba-doc/latest/user/installing.html#compatibility), it does not support _every_ platform that can run python.
Depending on your requirements for runtime and depending on your platform, it might be worth to try.

__By default, numba is disabled.__ Potrace with numba enabled can be installed with:

* `pip install potracer[numba]`

to make use of the speedup.

# Requirements
* numpy: for bitmap structures.

Expand Down
11 changes: 10 additions & 1 deletion potrace/potrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

import numpy as np

try:
from numba import njit
except ImportError:

def njit(*args, **kwargs):
return lambda f: f


POTRACE_TURNPOLICY_BLACK = 0
POTRACE_TURNPOLICY_WHITE = 1
POTRACE_TURNPOLICY_LEFT = 2
Expand Down Expand Up @@ -641,6 +649,7 @@ def findpath(bm: np.array, x0: int, y0: int, sign: bool, turnpolicy: int) -> _Pa
return _Path(pt, area, sign)


@njit()
def findnext(bm: np.array) -> Optional[Tuple[Union[int], int]]:
"""
/* find the next set pixel in a row <= y. Pixels are searched first
Expand Down Expand Up @@ -1262,7 +1271,7 @@ def _calc_lon(pp: _Path) -> int:
dk_y = sign(pt[k].y - pt[k1].y)
cur_x = pt[k1].x - pt[i].x
cur_y = pt[k1].y - pt[i].y
"""find largest integer j such that xprod(constraint[0], cur+j*dk) >= 0
"""find largest integer j such that xprod(constraint[0], cur+j*dk) >= 0
and xprod(constraint[1], cur+j*dk) <= 0. Use bilinearity of xprod. */"""
a = xprod(constraint0x, constraint0y, cur_x, cur_y)
b = xprod(constraint0x, constraint0y, dk_x, dk_y)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = potracer
version = 0.0.4
version = 0.0.5
description = Python Potrace
long_description_content_type=text/markdown
long_description = file: README.md
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
],
extras_require={
'cli': ["potrace-cli"],
'numba': ["numba"],
}
)
)