gpt4 book ai didi

rust - 等效于#[cfg(test)] 的基准?

转载 作者:行者123 更新时间:2023-11-29 08:31:07 24 4
gpt4 key购买 nike

更具体地说,我想为正常构建、测试构建和基准build设置不同的常量:

#[cfg(not(test))]
const X: usize = 16;
#[cfg(test)]
const X: usize = 10;
#[cfg(benchmarking)] // <-- this doesn't work, and would conflict with not(test)
const X: usize = 100;

最佳答案

您需要在 Cargo.toml 文件中添加一个功能(基准测试)部分。具体可以看Cargo documentation .

[features]
benchmarking = []

添加特性后,需要在代码中为该特性编写特定的基准测试代码

.
.
#[cfg(feature = "benchmarking")]
{
// count iterations check memory etc.
}
.
.

然后,如果你想将这些代码添加到编译中,你需要在构建时设置此功能,如下所示:

cargo build --features "benchmarking"

详细信息可以查看reference

关于rust - 等效于#[cfg(test)] 的基准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57607182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com