gpt4 book ai didi

clojure - Rust 与 Clojure 速度比较,Clojure 代码有何改进?

转载 作者:行者123 更新时间:2023-11-29 07:50:44 25 4
gpt4 key购买 nike

我将一段 Rust 代码示例翻译成 Clojure。

Rust(命令式和函数式):注意:为了清楚起见,此处将命令式代码和函数式代码放在一起。在测试中,我分别运行它们。

// The `AdditiveIterator` trait adds the `sum` method to iterators
use std::iter::AdditiveIterator;
use std::iter;
fn main() {
println!("Find the sum of all the squared odd numbers under 1000");
let upper = 1000u;

// Imperative approach
// Declare accumulator variable
let mut acc = 0;
// Iterate: 0, 1, 2, ... to infinity
for n in iter::count(0u, 1) {
// Square the number
let n_squared = n * n;

if n_squared >= upper {
// Break loop if exceeded the upper limit
break;
} else if is_odd(n_squared) {
// Accumulate value, if it's odd
acc += n_squared;
}
}
println!("imperative style: {}", acc);

// Functional approach
let sum_of_squared_odd_numbers =
// All natural numbers
iter::count(0u, 1).
// Squared
map(|n| n * n).
// Below upper limit
take_while(|&n| n < upper).
// That are odd
filter(|n| is_odd(*n)).
// Sum them
sum();
println!("functional style: {}", sum_of_squared_odd_numbers);
}

fn is_odd(n: uint) -> bool {
n % 2 == 1
}

Rust(命令式)时间:

~/projects/rust_proj $> time ./hof_imperative 
Find the sum of all the squared odd numbers under 1000
imperative style: 5456

real 0m0.006s
user 0m0.001s
sys 0m0.004s

~/projects/rust_proj $> time ./hof_imperative
Find the sum of all the squared odd numbers under 1000
imperative style: 5456

real 0m0.004s
user 0m0.000s
sys 0m0.004s

~/projects/rust_proj $> time ./hof_imperative
Find the sum of all the squared odd numbers under 1000
imperative style: 5456

real 0m0.005s
user 0m0.004s
sys 0m0.001s

Rust(功能)时间:

~/projects/rust_proj $> time ./hof 
Find the sum of all the squared odd numbers under 1000
functional style: 5456

real 0m0.007s
user 0m0.001s
sys 0m0.004s

~/projects/rust_proj $> time ./hof
Find the sum of all the squared odd numbers under 1000
functional style: 5456

real 0m0.007s
user 0m0.007s
sys 0m0.000s

~/projects/rust_proj $> time ./hof
Find the sum of all the squared odd numbers under 1000
functional style: 5456

real 0m0.007s
user 0m0.004s
sys 0m0.003s

Clojure:

(defn sum-square-less-1000 []
"Find the sum of all the squared odd numbers under 1000
"
(->> (iterate inc 0)
(map (fn [n] (* n n)))
(take-while (partial > 1000))
(filter odd?)
(reduce +)))

Clojure 时间:

user> (time (sum-square-less-1000))
"Elapsed time: 0.443562 msecs"
5456
user> (time (sum-square-less-1000))
"Elapsed time: 0.201981 msecs"
5456
user> (time (sum-square-less-1000))
"Elapsed time: 0.4752 msecs"
5456

问题:

  1. Clojure 中的(reduce +)(apply +) 有什么区别?
  2. 这段 Clojure 代码是惯用的方式吗?
  3. 我可以得出 Speed: Clojure > Rust imperative > Rust functional 的结论吗? Clojure 的性能让我大吃一惊。

最佳答案

如果您查看 + 的来源,您会看到 (reduce +)(apply +) 对于更高的参数计数是相同的。 (apply +) 针对 1 或 2 个参数版本进行了优化。

在大多数情况下,

(range) 将比 (iterate inc 0) 快得多。

partial 比简单的匿名函数慢,应该保留用于您不知道将提供多少个 args 的情况。

显示使用 criterium 进行基准测试的结果,我们可以看到应用这些更改会使执行时间减少 36%:

user> (crit/bench (->> (iterate inc 0)
(map (fn [n] (* n n)))
(take-while (partial > 1000))
(filter odd?)
(reduce +)))
WARNING: Final GC required 2.679748643529675 % of runtime
Evaluation count : 3522840 in 60 samples of 58714 calls.
Execution time mean : 16.954649 µs
Execution time std-deviation : 140.180401 ns
Execution time lower quantile : 16.720122 µs ( 2.5%)
Execution time upper quantile : 17.261693 µs (97.5%)
Overhead used : 2.208566 ns

Found 2 outliers in 60 samples (3.3333 %)
low-severe 2 (3.3333 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers
nil
user> (crit/bench (->> (range)
(map (fn [n] (* n n)))
(take-while #(> 1000 %))
(filter odd?)
(reduce +)))
Evaluation count : 5521440 in 60 samples of 92024 calls.
Execution time mean : 10.993332 µs
Execution time std-deviation : 118.100723 ns
Execution time lower quantile : 10.855536 µs ( 2.5%)
Execution time upper quantile : 11.238964 µs (97.5%)
Overhead used : 2.208566 ns

Found 2 outliers in 60 samples (3.3333 %)
low-severe 1 (1.6667 %)
low-mild 1 (1.6667 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers
nil

关于clojure - Rust 与 Clojure 速度比较,Clojure 代码有何改进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27034174/

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