Metadata-Version: 2.4
Name: turboshtein
Version: 0.0.5
Summary: Levenshtein edit distance computed quickly
Author-email: "Nathan A. Rooy" <nathanrooy@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/nathanrooy/turboshtein
Project-URL: Repository, https://github.com/nathanrooy/turboshtein
Project-URL: Issues, https://github.com/nathanrooy/turboshtein/issues
Keywords: levenshtein distance
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: C
Classifier: Operating System :: OS Independent
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: mypy>=1.0; extra == "dev"
Requires-Dist: ruff>=0.8.0; extra == "dev"
Dynamic: license-file

# turboshtein

[![PyPi Version](https://img.shields.io/pypi/v/turboshtein.svg?style=flat-square)](https://pypi.org/project/turboshtein/)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/turboshtein.svg?style=flat-square)](https://pypi.org/project/turboshtein/)
[![Build Status](https://img.shields.io/github/actions/workflow/status/nathanrooy/turboshtein/ci.yaml?branch=main&style=flat-square)](https://github.com/nathanrooy/turboshtein/actions/workflows/ci.yaml)
[![GitHub license](https://img.shields.io/github/license/nathanrooy/turboshtein?style=flat-square)](https://github.com/nathanrooy/turboshtein/blob/main/LICENSE)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square)](https://github.com/astral-sh/ruff)

## Background
I didn't study computer science so [bitwise operations](https://en.wikipedia.org/wiki/Bitwise_operation) have always been a bit of a gray area for me. I finally gave in and decided to spend some time reading up on the subject and ended up implementing the Myers bit-parallel algorithm<sup>[[1](https://github.com/nathanrooy/turboshtein#references)]</sup> for computing [Levenshtein edit distance](https://en.wikipedia.org/wiki/Levenshtein_distance). I was fairly happy with the results so I wrapped the original C code in Python and uploaded it to PyPI. Coincidentally, it's also pretty fast.

## Usage
Install via PyPI:

```sh
pip install turboshtein
```

Or from source:
```sh
pip install .
```

Computing levenshtein edit distance between two strings:
```py
>>> from turboshtein import levenshtein
>>> levenshtein("saturday", "sunday")
3
```

Note that there are several limitations, mainly:
- Max string length must be less than 64.
- ASCII or Latin-1 characters only.

## Performance

Values represent the number of string pairs processed per second (higher is better).

### Version 0.0.5


| library | version | turboshtein advantage | m=n=8 | m=n=16 | m=n=24 | m=n=32 | m=n=40 | m=n=48 | m=n=56 |
| ---: | ---: | :---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| turboshtein.levenshtein | 0.0.5 | --- | 19,685,039 | 13,094,009 | 9,949,417 | 7,949,915 | 6,488,589 | 5,269,281 | 4,605,111 |
| [rapidfuzz.distance.Levenshtein.distance](https://github.com/rapidfuzz/RapidFuzz) | 3.14.6 | **1.2x – 1.7x faster** | 11,616,641 | 9,109,890 | 7,422,064 | 6,272,703 | 5,220,116 | 4,418,830 | 3,923,236 |
| [Levenshtein.distance](https://github.com/rapidfuzz/Levenshtein) | 0.27.5 | **1.4x – 2.4x faster** | 8,104,003 | 6,769,142 | 5,748,639 | 5,019,556 | 4,191,919 | 3,771,687 | 3,404,689 |
| [jellyfish.levenshtein_distance](https://github.com/jamesturk/jellyfish) | 1.3.0 | **12x – 82x faster** | 1,649,960 | 549,253 | 294,319 | 138,237 | 95,645 | 72,144 | 56,138 |
| [textdistance.levenshtein](https://github.com/life4/textdistance) | 4.6.2 | **4.9x – 16x faster** | 1,219,580 | 1,128,859 | 1,077,474 | 1,038,781 | 1,005,222 | 971,589 | 946,850 |

Test setup:
- Apple M3
- macOS 26.6.2
- Python 3.13

### Version: 0.0.4

| library | version |     m=n=8 |    m=n=16 |    m=n=24 |    m=n=32 |    m=n=40 |    m=n=48 |    m=n=56 |
|----------------------------------------:|--------:|----------:|----------:|----------:|----------:|----------:|----------:|----------:|
|                 turboshtein.levenshtein |   0.0.4 | 7,625,612 | 6,134,367 | 5,160,757 | 4,455,692 | 3,897,966 | 3,477,885 | 3,147,237 |
| [Levenshtein.distance](https://github.com/maxbachmann/Levenshtein) |  0.20.5 | 6,448,035 | 5,335,239 | 4,556,680 | 4,034,226 | 3,550,695 | 3,211,664 | 2,910,581 |
| [rapidfuzz.distance.Levenshtein.distance](https://github.com/maxbachmann/RapidFuzz) |  2.11.1 | 6,446,954 | 5,268,454 | 4,560,213 | 4,027,110 | 3,540,574 | 3,215,206 | 2,912,386 |
| [jellyfish.levenshtein_distance](https://github.com/jamesturk/jellyfish) |   0.9.0 | 3,823,843 | 1,494,087 |   718,785 |   399,693 |   262,541 |   183,554 |   135,081 |
| [textdistance.levenshtein](https://github.com/life4/textdistance) |   4.5.0 |   444,305 |   413,950 |   411,441 |   401,539 |   395,777 |   389,462 |   385,233 |

Test setup:
- XPS 15 9570
- Intel i7-8750H
- Fedora Linux 36 (Workstation Edition)

## References
[1]: Gene Myers. 1999. A fast bit-vector algorithm for approximate string matching based on dynamic programming. J. ACM 46, 3 (May 1999), 395-415. https://doi.org/10.1145/316542.316550
