Created
May 6, 2026 16:28
-
-
Save omar391/841886c96cb449c252c6384c3de33502 to your computer and use it in GitHub Desktop.
Rust vs Go Actor Ping-Pong Benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package bench | |
| import "testing" | |
| func BenchmarkPingPong(b *testing.B) { | |
| ch1 := make(chan struct{}) | |
| ch2 := make(chan struct{}) | |
| go func() { | |
| for { | |
| <-ch1 | |
| ch2 <- struct{}{} | |
| } | |
| }() | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| ch1 <- struct{}{} | |
| <-ch2 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use criterion::{criterion_group, criterion_main, Criterion}; | |
| use tokio::sync::mpsc; | |
| use tokio::runtime::Runtime; | |
| use std::time::Instant; | |
| fn bench_ping_pong(c: &mut Criterion) { | |
| let rt = Runtime::new().unwrap(); | |
| let _guard = rt.enter(); | |
| let (tx1, mut rx1) = mpsc::channel::<()>(1); | |
| let (tx2, mut rx2) = mpsc::channel::<()>(1); | |
| rt.spawn(async move { | |
| while let Some(_) = rx1.recv().await { | |
| if tx2.send(()).await.is_err() { | |
| break; | |
| } | |
| } | |
| }); | |
| c.bench_function("ping_pong_rust", |b| { | |
| b.iter_custom(|iters| { | |
| rt.block_on(async { | |
| let start = Instant::now(); | |
| for _ in 0..iters { | |
| let _ = tx1.send(()).await; | |
| let _ = rx2.recv().await; | |
| } | |
| start.elapsed() | |
| }) | |
| }) | |
| }); | |
| } | |
| criterion_group!(benches, bench_ping_pong); | |
| criterion_main!(benches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment