fastdiff
is a re-implementation of difflib in pure Rust compiled to WebAssembly to speedup different language integrations:
To install fastdiff
in Python, you just need to do:
pip install fastdiff
And then, use it in Python like this:
from fastdiff import compare
str1 = 'hello\nwasm\n'
str2 = 'hello\npython\n'
print(compare(str1, str2))
When comparing strings with 300 lines, the WebAssembly based approach is about 75 times faster than the pure Python approach.
------------------------------------------------------------------------------------------------ benchmark: 2 tests ------------------------------------------------------------------------------------------------
Name (time in ms) Min Max Mean StdDev Median IQR Outliers OPS Rounds Iterations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_benchmark_content_native 137.9896 (1.0) 145.8701 (1.0) 141.2983 (1.0) 2.8708 (1.0) 140.5982 (1.0) 4.2806 (1.0) 3;0 7.0772 (1.0) 7 1
test_benchmark_content_base 10,665.8390 (77.29) 11,141.3282 (76.38) 10,837.7984 (76.70) 183.8493 (64.04) 10,817.5194 (76.94) 206.9630 (48.35) 1;0 0.0923 (0.01) 5 1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
For building the WebAssembly file, you need Rust and the wasm32
target.
# Install Rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Add wasm32 target
rustup target add wasm32-unknown-unknown
And then, run:
make build
For testing in Python you can do:
make test_python