Rust vs Python: The Same Image, Two Very Different Run Times

We wrote two small programs to draw the same Mandelbrot fractal, a detailed pattern built by repeating a calculation for every pixel: one program in Python and one in Rust. Each checks the same points, uses the same colours, and saves the same image. Their output files matched byte for byte. What changed was the time and memory needed to produce them.

The picture on this article’s cover comes from that test. The chart below shows the measured time for the larger image.

Bar chart: Python took 2.98 seconds and Rust took 0.11 seconds to render the same 960 by 640 Mandelbrot image; median of five runs.

What we tested

Both programs used only their language's standard library. They ran on one CPU thread, without a GPU or an image-processing package. We asked each to render a 960 × 640-pixel fractal with a maximum of 220 iterations per point. We also tried a smaller 160 × 100-pixel image.

We ran the programs on a laptop with an Intel Core i5-1135G7 processor, using Python 3.12.3 and Rust 1.100.0. The Rust program was built with Cargo's optimized --release setting. After one warm-up run each, we timed five runs in alternating order and report the median. The times include starting the program and writing its image file. We measured Rust's initial build separately.

Image Python median Rust median Difference
160 × 100 pixels 0.057 s 0.0033 s Rust about 17× faster
960 × 640 pixels 2.98 s 0.111 s Rust about 27× faster

Process startup makes up a larger share of the small test, so the larger image gives the clearer picture of sustained calculation work.

For the larger image, peak resident memory was about 11.0 MiB for Python and 3.6 MiB for Rust. These are measurements of these processes on this laptop, rather than memory guarantees for either language. The matching image hashes confirm that the speed difference did not come from producing different pictures.

Where Rust pulled ahead

This task spends most of its time repeating arithmetic inside a pixel-by-pixel loop. Rust turns the program into optimized native code before it runs; Cargo's release profile is designed for that trade-off. In our test, the optimized Rust program completed the larger render about 27 times faster than the straightforward Python version.

That result is useful for workloads with millions of small calculations, especially when the program will run repeatedly. A security tool that processes a large stream of records may have similar performance concerns, although this fractal test does not measure a security tool.

Where Python remains appealing

Rust took 0.77 seconds to build for the first time on this laptop. Python needed no separate build step: we could edit the file and run it immediately. For a tiny, one-off task, that quick edit-and-run cycle may matter more than the time saved during execution. Python's own overview highlights that development advantage.

This was also a comparison with ordinary Python loops, not every way to write a fast Python program. Libraries such as NumPy move many array operations into compiled code. A Python program built around such libraries could perform very differently; we did not test one here.

Rust also has compile-time ownership checks that are relevant when building reliable systems software. Our image test measured speed and process memory, not security or code quality.

What we take from it

For this repeated, calculation-heavy task, Rust's advantage was clear and the final image was identical. Python offered a simpler route from source file to first run. The choice between them depends on the work: how often it runs, how much computation it does, and whether a Python library already handles the expensive part.

If you would like to contribute to CyberS&D's future research, please consider using the contribute form on this website.

Previous
Previous

Boston Dynamics’ Spot: Where Robotics Meets Practical AI

Next
Next

NotHoly Specter’s API and MCP: Two Local Security Tests