Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve performance #2

Merged
merged 1 commit into from
Apr 30, 2024
Merged

improve performance #2

merged 1 commit into from
Apr 30, 2024

Conversation

nidhoggfgg
Copy link
Contributor

init a Vec with vec![] is much faster than for_each (10 times).
and there is a remove(0) on Vec, it's very slow, use Deque is better.

the bench of the deque and vec

// remove the for loop to see the difference in initialization Vector vs Deque

use std::collections::VecDeque;

use criterion::{criterion_group, criterion_main, Criterion};
use glam::Vec2;

fn vec() {
    let mut v = vec![Vec2::ZERO; 100];
    for _ in 0..100 {
        v.remove(0);
    }
}

fn deque() {
    let mut v = VecDeque::from(vec![Vec2::ZERO; 100]);
    for _ in 0..100 {
        v.pop_front();
    }
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("vec", |b| b.iter(|| vec()));
    c.bench_function("deque", |b| b.iter(|| deque()));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

result, 117.59ps vs 888180ps

vec                     time:   [886.04 ns 888.18 ns 891.06 ns]
deque                   time:   [117.53 ps 117.59 ps 117.66 ps]

the bench code of vec init way:

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use glam::Vec2;

fn vec_init_fn(pos: Vec2, len: usize) {
    let mut trail = Vec::with_capacity(len);
    (0..len).for_each(|_| trail.push(pos));
}

fn vec_init_macro(pos: Vec2, len: usize) {
    vec![pos; len];
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("macro", |b| {
        b.iter(|| vec_init_macro(black_box(Vec2::ZERO), black_box(100)))
    });
    c.bench_function("fn", |b| {
        b.iter(|| vec_init_fn(black_box(Vec2::ZERO), black_box(100)))
    });
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

result, 22ns vs 209ns

macro                   time:   [22.529 ns 22.612 ns 22.698 ns]
fn                      time:   [209.63 ns 209.74 ns 209.88 ns]

btw, maybe you can try "early return", bc the indent of the Terminal::render is so much.
and programming such a particle system is wonderful, i would think make a braille code version in rsille

@Wayoung7
Copy link
Owner

Yeah, deque is faster and more suitable for this use case. I didn't even think of VecDeque at that time when I was programing this. Thanks for your suggestion.

@Wayoung7 Wayoung7 merged commit cafb248 into Wayoung7:master Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants