为什么64bits乘法的基准性能与32bits乘法相当?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
标准
错误。编译器仍将消除乘法。为了使其工作,您需要功能的结果来依赖于他们的参数。最简单的方法是使用一轮并让
Criterion
担心其余的:请参阅用户指南。
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:See the user guide.