rusticstuff / simdutf8

SIMD-accelerated UTF-8 validation for Rust.
Other
522 stars 25 forks source link

Question. Speed on large inputs. #82

Open scheglov opened 11 months ago

scheglov commented 11 months ago

Why do I get only about 12 GB/s with this manual benchmark?

use std::time::Instant;

use simdutf8::basic::from_utf8;

fn main() {
    let mut vec: Vec<u8> = Vec::new();

    for i in 0..1024 * 1024 * 10 {
        vec.push((i % 10) as u8 + b'0');
    }

    // println!("{:?}", vec);

    println!("{}", from_utf8(b"I \xE2\x9D\xA4\xEF\xB8\x8F UTF-8!").unwrap());

    let start = Instant::now();

    let decoded = from_utf8(vec.as_slice()).unwrap();
    // let decoded = std::str::from_utf8(vec.as_slice()).unwrap();
    println!("length: {}", decoded.len());

    let mut elapsed = Instant::now().duration_since(start);
    println!("Elapsed time: {:?}", elapsed);
    let giga = 1024 * 1024 * 1024;
    println!("Speed: {:?} GB/s", 1000000.0 / (elapsed.as_micros() as f64) * (vec.len() as f64) / (giga as f64));
}

When I run the benchmark (slightly patched), I get about 80 GB/s.

1-latin/1048576         time:   [12.134 µs 12.144 µs 12.155 µs]
                        thrpt:  [80.339 GiB/s 80.416 GiB/s 80.481 GiB/s]
scheglov commented 11 months ago

cargo 1.65.0 (4bc8f24d3 2022-10-20) and Apple M1 Pro