Which is the fastest execution to write dynamic data to a socket among these options?

⚓ Rust    📅 2025-06-25    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 48 days ago. The information described in this article may have changed.

Hello, I'm searching for the fastest one for single threaded execution. Which one?

These are pseudo code (the syntax is not correct)

Number 1

let mut buf = String::with_capacity(1024)

fn run (input: &str) {
    write!(&mut buf, "data: {}", input)
    socket.write_all(buf.as_bytes())
}

let input = format!("number {}", 100)
run(&input)

Number 2

let mut buf = vec![0u8; 1024]

fn run (input: &str) {
    write!(&mut buf, "data: {}", input)
    socket.write_all(&buf)
}

let input = format!("number {}", 100)
run(&input)

Number 3

let mut buf = String::with_capacity(1024)

fn run (input: String) {
    write!(&mut buf, "data: {}", input)
    socket.write_all(buf.as_bytes())
}

let input = format!("number {}", 100)
run(input)

Number 4

let mut buf = vec![0u8; 1024]

fn run (input: String) {
    write!(&mut buf, "data: {}", input)
    socket.write_all(&buf)
}

let input = format!("number {}", 100)
run(input)

Number 5

let mut buf = vec![0u8; 1024]

fn run (input: &[u8]) {
    buf.extend_from_slices(input)
    socket.write_all(&buf)
}

let input = format!("number {}", 100)
run(input.as_bytes())

Number 6

let mut buf = vec![0u8; 1024]

fn run (mut input: Vec<u8>) {
    buf.append(&mut input)
    socket.write_all(&buf)
}

let input = format!("number {}", 100)
run(input.into_bytes())

Number 7

let mut buf = bytes::BytesMut::with_capacity(1024)

fn run (input: &[u8]) {
    buf.extend_from_slices(input)
    socket.write_all(&buf)
}

let input = format!("number {}", 100)
run(input.as_bytes())

Number 8, 9, 10 .... are the same except : using std::io::IoSlice and write_vectorized

10 posts - 4 participants

Read full topic

🏷️ rust_feed