A simple benchmark project to compare PHP and Rust performance in computing prime numbers.
This project demonstrates how PHP can call a compiled Rust binary via shell_exec() and measure total execution time, while Rust performs the heavy computation efficiently.
With No Jit and Opcache
With Jit and Opcache
How to set Opcache and JIT
| Language | Method | Description |
|---|---|---|
| PHP | Pure PHP | Calculates prime numbers using a simple loop and checks for divisibility. Single-threaded. |
| Rust | Binary (called via PHP) | Compute efficiently. |
You can view the live demo here:
- Main page: https://phprust-bench.porrapat.com/
- Rust call example: https://phprust-bench.porrapat.com/call_rust.php
This project runs on the smallest Droplet size from DigitalOcean (region: SGP1):
PHP-Rust-Bench
512 MB RAM / 10 GB Disk / SGP1 - Ubuntu 25.10 x64
You can also test the benchmark with a custom limit by appending a limit parameter (default = 500,000):
https://phprust-bench.porrapat.com/?limit=100000
https://phprust-bench.porrapat.com/?limit=400000
https://phprust-bench.porrapat.com/?limit=800000
https://phprust-bench.porrapat.com/call_rust.php?limit=100000
https://phprust-bench.porrapat.com/call_rust.php?limit=400000
https://phprust-bench.porrapat.com/call_rust.php?limit=800000
https://phprust-bench.porrapat.com/call_rust.php?limit=2000000Also with Sieve of Eratosthenes:
Thank you from Katopz
https://phprust-bench.porrapat.com/eratos.php?limit=100000
https://phprust-bench.porrapat.com/eratos.php?limit=400000
https://phprust-bench.porrapat.com/eratos.php?limit=800000
https://phprust-bench.porrapat.com/call_rust_eratos.php?limit=100000
https://phprust-bench.porrapat.com/call_rust_eratos.php?limit=400000
https://phprust-bench.porrapat.com/call_rust_eratos.php?limit=800000
https://phprust-bench.porrapat.com/call_rust_eratos.php?limit=2000000The limit defines the upper bound of numbers to check for primes.
The PHP is capped at 2,000,000 but Rust is at 20,000,000 to prevent memory overflow or performance degradation on smaller servers.
cargo build --release --bin phprust-bench
cargo build --release --bin phprust-bench-eratosThis produces target/release/phprust-bench (or .exe on Windows).
We locked output at 2,000,000 to protect PHP server. Otherwise for Rust is locked at 20,000,000. You can uncommented there.
Rust found 148933 primes up to 2000000 in 0.108 seconds.
Total call time from PHP: 0.154 seconds.
- ✅ Windows: uses
.exepath (escaped with\\) - ✅ Linux / macOS: uses
./target/release/phprust-bench
MIT License © 2025 Porrapat Petchdamrongskul
Special thanks to Katopz
for suggesting the Sieve of Eratosthenes algorithm.
I use another server to run and connect it using gRPC (4 vCPU)
And it's max (48 vCPU) what we can do




