为什么64bits乘法的基准性能与32bits乘法相当?

发布于 2025-02-01 00:36:47 字数 1086 浏览 3 评论 0原文

这是的扩展。我正在为这些基准测试。 64bits乘法的性能等于32位乘法。在以前的问题中,人们建议使用基准测试,并且在使用后,我仍然会得到相同的性能。请注意,我是新来的生锈,所以如果我做错了什么,请告诉我。这是我的简单基准标记文件,

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

pub fn test_64_mul(test_num: u64){    
    for _ in 1..20000{
        let mut _prod = test_num as u128 * test_num as u128;
 
    }
}

pub fn test_32_mul(test_num: u32){
    for _ in 1..20000{
        let mut _prod = test_num as u64 * test_num as u64;
 
    }
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("mul 64", |b| b.iter(|| test_64_mul(black_box(12345678653435363454))));
    c.bench_function("mul 32", |b| b.iter(|| test_64_mul(black_box(1234565755))));
}

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

现在我做货物台输出是:

MUL 64时间:[312.47 PS 312.66 PS 312.93 PS]

mul 32时间:[312.56 PS 312.75 PS 312.999999 ps]

This is extension of this question. I am using rust for these benchmarks. Performance of 64bits multiplication is equal to 32bit multiplication. IN previous question people suggested to use benchmarking and after using that I am still getting same performance. Please note I am new with rust so please let me know if I am doing anything wrong. Here is my simple bench marking file

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

pub fn test_64_mul(test_num: u64){    
    for _ in 1..20000{
        let mut _prod = test_num as u128 * test_num as u128;
 
    }
}

pub fn test_32_mul(test_num: u32){
    for _ in 1..20000{
        let mut _prod = test_num as u64 * test_num as u64;
 
    }
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("mul 64", |b| b.iter(|| test_64_mul(black_box(12345678653435363454))));
    c.bench_function("mul 32", |b| b.iter(|| test_64_mul(black_box(1234565755))));
}

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

Now when I do cargo bench output is:

mul 64 time: [312.47 ps 312.66 ps 312.93 ps]

mul 32 time: [312.56 ps 312.75 ps 312.99 ps]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2025-02-08 00:36:47

您正在使用标准错误。编译器仍将消除乘法。

为了使其工作,您需要功能的结果来依赖于他们的参数。最简单的方法是使用一轮并让Criterion担心其余的:

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

pub fn test_64_mul(test_num: u64) -> u128 {
    test_num as u128 * test_num as u128
}

pub fn test_32_mul(test_num: u32) -> u64 {
    test_num as u64 * test_num as u64
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("mul 64", |b| b.iter(|| test_64_mul(black_box(12345678653435363454))));
    c.bench_function("mul 32", |b| b.iter(|| test_64_mul(black_box(1234565755))));
}

请参阅用户指南

You are using criterion incorrectly. The compiler will still eliminate the multiplication.

For it to work, you need the result of the functions to depend on the arguments to them. The easiest way is to write use one round and let criterion worry about the rest:

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

pub fn test_64_mul(test_num: u64) -> u128 {
    test_num as u128 * test_num as u128
}

pub fn test_32_mul(test_num: u32) -> u64 {
    test_num as u64 * test_num as u64
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("mul 64", |b| b.iter(|| test_64_mul(black_box(12345678653435363454))));
    c.bench_function("mul 32", |b| b.iter(|| test_64_mul(black_box(1234565755))));
}

See the user guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文