frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

C++ ranges/views vs. Rust iterator

2•bijan7•8h ago
it seems there is a quite a bit of gap between the performance of Rust iterator and C++ ranges/views unless I am missing something.

https://godbolt.org/z/v76rcEb9n https://godbolt.org/z/YG1dv4qYh

Rust: <code> use std::time::Instant;

fn expand_iota_views(input: &[i32]) -> impl Iterator<Item = i32> + '_ { input .iter() .flat_map(|&n| 1..=n) .flat_map(|n| 1..=n) .flat_map(|n| 1..=n) }

fn main() { let input: Vec<i32> = (0..=50).collect();

    let sample_result: Vec<i32> = expand_iota_views(&input).collect();
    println!("Rust Result count: {}", sample_result.len());

    let start = Instant::now();
    let mut total_count = 0;
    for _ in 0..1000 {
        let result = expand_iota_views(&input);
        total_count += result.count();
    }
    let duration = start.elapsed();

    println!("Rust Total count (1000 iterations): {}", total_count);
    println!("Rust Total time: {} microseconds", duration.as_micros());
    println!(
        "Rust Average per iteration: {:.2} microseconds",
        duration.as_micros() as f64 / 1000.0
    );
} </code>

Output: Rust Result count: 292825 Rust Total count (1000 iterations): 292825000 Rust Total time: 1025 microseconds Rust Average per iteration: 1.02 microseconds

C++: <code> #include <chrono> #include <iostream> #include <numeric> #include <ranges> #include <vector>

inline auto expandIotaViews(const std::vector<int>& input) { auto iota_transform = [](const int number) { return std::views::iota(1, number + 1); };

    return input 
                | std::views::transform(iota_transform) 
                | std::views::join 
                | std::views::transform(iota_transform) 
                | std::views::join
                | std::views::transform(iota_transform) 
                | std::views::join;
}

int main() { std::vector<int> input(51); std::iota(input.begin(), input.end(), 0);

    auto sample_result = expandIotaViews(input);
    std::vector<int> result_vec;
    for (auto val : sample_result) {
        result_vec.push_back(val);
    }

    std::cout << "C++ Result count: " << result_vec.size() << std::endl;

    auto start = std::chrono::high_resolution_clock::now();
    size_t total_count = 0;
    for (int i = 0; i < 1000; ++i) {
        auto result = expandIotaViews(input);
        total_count += std::ranges::distance(result);
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration =
        std::chrono::duration_cast<std::chrono::microseconds>(end - start);

    std::cout << "C++ Total count (1000 iterations): " << total_count
              << std::endl;
    std::cout << "C++ Total time: " << duration.count() << " microseconds"
              << std::endl;
    std::cout << "C++ Average per iteration: " << duration.count() / 1000.0
              << " microseconds" << std::endl;

    return 0;
} </code>

Output: C++ Result count: 292825 C++ Total count (1000 iterations): 292825000 C++ Total time: 174455 microseconds C++ Average per iteration: 174.455 microseconds

Comments

npalli•3h ago
Most likely explanation: In the Rust version, the compiler can see that expand_iota_views(&input).count() is a pure, loop-invariant expression since input never changes. It folds the whole computation and effectively turns the loop into "add the constant a thousand times". Make it do some work and it will line up to the C++ version.

Paid $2400 to Cloudflare, support refuses to help

117•thekonqueror•12h ago•21 comments

Aura – Detecting Fake Cell Towers with RF Fingerprinting AI

6•sadpig70•3h ago•0 comments

Ask HN: What Single File Web Apps do you know of?

4•calebm•6h ago•5 comments

How WASM DB and worker messaging helped me handle 500MB in 2s in browser

4•vinserello•11h ago•1 comments

What problems are worth solving?

6•KopyWasTaken•7h ago•2 comments

C++ ranges/views vs. Rust iterator

2•bijan7•8h ago•1 comments

Mirai Variant "Gayfemboy" Infecting 15K+ Devices Daily – Mitigation Ideas?

7•garduno_AA•9h ago•3 comments

GitHub Attack – branches sending secrets to webhook

6•danieldspx•9h ago•2 comments

Ask HN: What's a good 3D Printer for sub $1000?

5•lucideng•11h ago•4 comments

Ask HN: Does anyone have any screenshots of fucked company?

3•iamflimflam1•12h ago•4 comments

Cloudflare Security Mistriages on Account Takeover

4•matured_kazama•13h ago•0 comments

Ask HN: Costs for US sales tax compliance for a two-sided marketplace

3•throway-9998888•16h ago•1 comments

Google Ends Support for Lynx Browser

102•zhenyi•5d ago•43 comments

Ask HN: Getting over Burnout with Imposter Syndrome

20•chrsig•2d ago•5 comments

Git Without Stash/Tags

3•birb07•1d ago•5 comments

Lost $300 due to an API key leak from "vibe coding" – Learn from my mistake

4•liulanggoukk•1d ago•11 comments

Ask HN: Who wants to be hired? (September 2025)

124•whoishiring•2w ago•390 comments

You've reached the end!