如果 f64::powf 是一个 const 函数,那么编译器应将 3.0 * 33.0f64.powf(0.5) 之类的内容转换为单个固定值。
虽然lazy_static 可用于解决此问题,但使用lazy_statics 是有代价的,因为它们旨在支持的不仅仅是简单的浮点常量。
您可以通过使用 Criterion 对两个实现进行基准测试来查看此成本:
pub mod ls {
use lazy_static::lazy_static; // 1.4.0
lazy_static! {
//TODO: Should this be a pow(1.0/3.0)?
pub static ref cbrt_33_mul_3: f64 = 3.0 * 33.0f64.powf(0.5);
pub static ref tribonacci_constant: f64 = 1.0
+ (19.0 - *cbrt_33_mul_3).powf(1.0 / 3.0)
+ (19.0 + *cbrt_33_mul_3).powf(1.0 / 3.0);
}
pub fn tribonacci(n: f64) -> f64 {
return (
(*tribonacci_constant / 3.0).powf(n)
/ (
(4.0 / 3.0)
* *tribonacci_constant
- (1.0 / 9.0)
* tribonacci_constant.powf(2.0) - 1.0
)
).round();
}
}
pub mod hc {
pub fn tribonacci(n: f64) -> f64 {
let p = 1.839286755214161;
let s = 0.3362281169949411;
return (s * p.powf(n)).round();
}
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("trib 5.1 ls", |b| b.iter(|| ls::tribonacci(black_box(5.1))));
c.bench_function("trib 5.1 hc", |b| b.iter(|| hc::tribonacci(black_box(5.1))));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
成本很小,但如果这是在您的核心循环中,则可能会很重要。
在我的机器上,我得到(在删除不相关的行之后)
trib 5.1 ls time: [47.946 ns 48.832 ns 49.796 ns]
trib 5.1 hc time: [38.828 ns 39.898 ns 41.266 ns]
这大约是 20% 的差异。
如果您不喜欢在代码中使用硬编码常量,您实际上可以在构建时使用 build.rs 脚本生成这些常量。
我的完整基准测试示例如下所示:
build.rs
use std::env;
use std::fs;
use std::path::Path;
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("constants.rs");
//TODO: Should this be a pow(1.0/3.0)?
let cbrt_33_mul_3: f64 = 3.0 * 33.0f64.powf(0.5);
let tribonacci_constant: f64 = 1.0
+ (19.0 - cbrt_33_mul_3).powf(1.0 / 3.0)
+ (19.0 + cbrt_33_mul_3).powf(1.0 / 3.0);
let p = tribonacci_constant / 3.0;
let s = 1.0 / (
(4.0 / 3.0)
* tribonacci_constant
- (1.0 / 9.0)
* tribonacci_constant.powf(2.0) - 1.0
);
fs::write(
&dest_path,
format!("\
pub mod tribonacci {{\n\
pub const P: f64 = {:.32};\n\
pub const S: f64 = {:.32};\n\
}}\n", p, s)
).unwrap();
println!("cargo:rerun-if-changed=build.rs");
}
src/lib.rs
pub mod constants {
include!(concat!(env!("OUT_DIR"), "/constants.rs"));
}
pub mod ls {
use lazy_static::lazy_static; // 1.4.0
lazy_static! {
//TODO: Should this be a pow(1.0/3.0)?
pub static ref cbrt_33_mul_3: f64 = 3.0 * 33.0f64.powf(0.5);
pub static ref tribonacci_constant: f64 = 1.0
+ (19.0 - *cbrt_33_mul_3).powf(1.0 / 3.0)
+ (19.0 + *cbrt_33_mul_3).powf(1.0 / 3.0);
}
pub fn tribonacci(n: f64) -> f64 {
return (
(*tribonacci_constant / 3.0).powf(n)
/ (
(4.0 / 3.0)
* *tribonacci_constant
- (1.0 / 9.0)
* tribonacci_constant.powf(2.0) - 1.0
)
).round();
}
}
pub mod hc {
pub fn tribonacci(n: f64) -> f64 {
let p = super::constants::tribonacci::P;
let s = super::constants::tribonacci::S;
return (s * p.powf(n)).round();
}
}
benches/my_benchmark.rs
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rust_gen_const_vs_lazy_static::ls;
use rust_gen_const_vs_lazy_static::hc;
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("trib 5.1 ls", |b| b.iter(|| ls::tribonacci(black_box(5.1))));
c.bench_function("trib 5.1 hc", |b| b.iter(|| hc::tribonacci(black_box(5.1))));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Cargo.toml
[package]
name = "rust_gen_const_vs_lazy_static"
version = "0.1.0"
edition = "2018"
[dependencies]
"lazy_static" = "1.4.0"
[dev-dependencies]
criterion = "0.3"
[[bench]]
name = "my_benchmark"
harness = false
$OUTDIR/constants.rs(生成)
pub mod tribonacci {
pub const P: f64 = 1.83928675521416096216853475198150;
pub const S: f64 = 0.33622811699494109527464047459944;
}
正如 Dilshod Tadjibaev 所建议的,使用 proc-macros 可以获得类似的结果,尽管在这种情况下需要做更多的工作。这提供了与构建时生成完全相同的速度。
为了进行设置,我为宏 trib_macros 创建了一个新的 crate,因为 proc-macros 需要在它们自己的 crate 中。这个新的 crate 只包含两个文件 Cargo.toml 和 src/lib.rs
Cargo.toml
[package]
name = "trib_macros"
version = "0.1.0"
edition = "2018"
[lib]
proc-macro = true
src/lib.rs
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro]
pub fn tp(_item: TokenStream) -> TokenStream {
let cbrt_33_mul_3: f64 = 3.0 * 33.0f64.powf(0.5);
let tribonacci_constant: f64 = 1.0
+ (19.0 - cbrt_33_mul_3).powf(1.0 / 3.0)
+ (19.0 + cbrt_33_mul_3).powf(1.0 / 3.0);
let p = tribonacci_constant / 3.0;
format!("{}f64",p).parse().unwrap()
}
#[proc_macro]
pub fn ts(_item: TokenStream) -> TokenStream {
let cbrt_33_mul_3: f64 = 3.0 * 33.0f64.powf(0.5);
let tribonacci_constant: f64 = 1.0
+ (19.0 - cbrt_33_mul_3).powf(1.0 / 3.0)
+ (19.0 + cbrt_33_mul_3).powf(1.0 / 3.0);
let s = 1.0 / (
(4.0 / 3.0)
* tribonacci_constant
- (1.0 / 9.0)
* tribonacci_constant.powf(2.0) - 1.0
);
format!("{}f64",s).parse().unwrap()
}
那我们需要调整原来 crate 的Cargo.toml 把这个拉进去。
[dependencies]
...
trib_macros = { path = "path/to/trib_macros" }
而且最后用起来比较干净:
pub mod mc {
use trib_macros::{ts,tp};
pub fn tribonacci(n: f64) -> f64 {
return (ts!() * tp!().powf(n)).round();
}
}
肯定有一种更简洁的方式来输出浮点文字标记,但我找不到。
您可以在 https://github.com/mikeando/rust_code_gen_example 找到这些测试的完整存储库