Making Python faster with Rust
The most important part of the article seems to be that this Python code is taking &quot;an avg of 293.41ms per iteration&quot;:<p><pre><code>        def find_close_polygons(
            polygon_subset: List[Polygon], point: np.array, max_dist: float
        ) -&gt; List[Polygon]:
            close_polygons = []
            for poly in polygon_subset:
                if np.linalg.norm(poly.center - point) &lt; max_dist:
                    close_polygons.append(poly)

            return close_polygons
</code></pre>
And after replacing it with this Rust code, it is taking &quot;an avg of 23.44ms per iteration&quot;:<p><pre><code>        use pyo3::prelude::*;
        use ndarray_linalg::Norm;
        use numpy::PyReadonlyArray1;

        #[pyfunction]
        fn find_close_polygons(
            py: Python&lt;&#x27;_&gt;,
            polygons: Vec&lt;PyObject&gt;,
            point: PyReadonlyArray1&lt;f64&gt;,
            max_dist: f64,
        ) -&gt; PyResult&lt;Vec&lt;PyObject&gt;&gt; {
            let mut close_polygons = vec![];
            let point = point.as_array();
            for poly in polygons {
                let center = poly
                    .getattr(py, &quot;center&quot;)?
                    .extract::&lt;PyReadonlyArray1&lt;f64&gt;&gt;(py)?
                    .as_array()
                    .to_owned();

                if (center - point).norm() &lt; max_dist {
                    close_polygons.push(poly)
                }
            }

            Ok(close_polygons)
        }
</code></pre>
Why is the Rust version 13x faster than the Python version?