我想根据机器的核心生成一定数量的任务。 Rust中有什么东西可以找到核心数,或者我应该只运行外部命令并解析输出?
答案 0 :(得分:23)
现在有一个箱子可以做到这一点:https://crates.io/crates/num_cpus
将此添加到您的DECLARE @Month AS INT = 5 --Set the MONTH for which you want to generate the Calendar.
DECLARE @Year AS INT = 2015 --Set the YEAR for which you want to generate the Calendar.
DECLARE @StartDate AS DATETIME = CONVERT(VARCHAR,@Year) + RIGHT('0' + CONVERT(VARCHAR,@Month),2) + '01'
DECLARE @EndDate AS DATETIME = DATEADD(DAY,-1,DATEADD(MONTH,1,@StartDate));
SELECT
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 1
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Sunday,
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 2
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Monday,
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 3
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Tuesday,
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 4
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Wednesday,
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 5
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Thursday,
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 6
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Friday,
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 7
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Saturday
FROM master.dbo.spt_values v
WHERE DATEADD(DD,number,@StartDate) BETWEEN @StartDate AND
DATEADD(DAY,-1,DATEADD(MONTH,1,@StartDate)) AND
v.type = 'P'
GROUP BY DATEPART(WEEK, DATEADD(DD,number,@StartDate))
:
Cargo.toml
然后在你的来源:
[dependencies]
num_cpus = "0.2"
答案 1 :(得分:4)
您可以使用std::os::num_cpus
。例如:
fn main() {
println!("{}", std::os::num_cpus());
}
答案 2 :(得分:3)
现在可以使用:
std::os::num_cpus
pub fn num_cpus() -> uint
Rust的版本:
$ rustc --version
rustc 0.13.0-nightly (d91a015ab 2014-11-14 23:37:27 +0000)
参考: