使用瞬态的面向性能的解决方案,reduce-kv
以及迭代较小映射的大小检查:
(defn merge-common-with [f m1 m2]
(let [[a b] (if (< (count m1) (count m2))
[m1 m2]
[m2 m1])]
(persistent!
(reduce-kv (fn [out k v]
(if (contains? b k)
(assoc! out k (f (get a k) (get b k)))
out))
(transient {})
a))))
在 REPL 中,使用问题文本中的示例数据:
(merge-common-with + (data "Bob") (data "Jane"))
;= {"A" 5.5, "B" 6.0}
请注意,虽然我希望上述方法在许多情况下是最快的方法,但我肯定会使用您的实际用例的典型数据进行基准测试。这是一个基于标准的基准测试,使用data
来自问题文本(merge-common-with
在这里获胜):
(require '[criterium.core :as c])
(def a (data "Bob"))
(def b (data "Jane"))
;; Hendekagon's elegant approach amended to select-keys on both sides
(defn merge-common-with* [f a b]
(merge-with f
(select-keys a (keys b))
(select-keys b (keys a))))
;; benchmarks for three approaches follow, fastest to slowest
(c/bench (merge-common-with + a b))
Evaluation count : 74876640 in 60 samples of 1247944 calls.
Execution time mean : 783.233604 ns
Execution time std-deviation : 7.660391 ns
Execution time lower quantile : 771.514052 ns ( 2.5%)
Execution time upper quantile : 802.622953 ns (97.5%)
Overhead used : 1.266543 ns
Found 3 outliers in 60 samples (5.0000 %)
low-severe 3 (5.0000 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers
(c/bench (merge-matching + a b)) ; amalloy's approach
Evaluation count : 57320640 in 60 samples of 955344 calls.
Execution time mean : 1.047921 µs
Execution time std-deviation : 16.221173 ns
Execution time lower quantile : 1.025001 µs ( 2.5%)
Execution time upper quantile : 1.076081 µs (97.5%)
Overhead used : 1.266543 ns
(c/bench (merge-common-with* + a b))
WARNING: Final GC required 3.4556868188006065 % of runtime
Evaluation count : 33121200 in 60 samples of 552020 calls.
Execution time mean : 1.862483 µs
Execution time std-deviation : 26.008801 ns
Execution time lower quantile : 1.821841 µs ( 2.5%)
Execution time upper quantile : 1.914336 µs (97.5%)
Overhead used : 1.266543 ns
Found 1 outliers in 60 samples (1.6667 %)
low-severe 1 (1.6667 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers