4 は、引数が多い場合と少ない場合の効率のバランスを取るようです。
例として:
(defn vector-few
([] [])
([ & args ] (. clojure.lang.LazilyPersistentVector (create args))))
(defn vector-many
([] [])
([a] [a])
([a b] [a b])
([a b c] [a b c])
([a b c d] [a b c d])
([a b c d e] [a b c d e])
([a b c d e f] [a b c d e f])
([a b c d e f & args] (. clojure.lang.LazilyPersistentVector (create (cons a (cons b (cons c (cons d (cons e (cons f args))))))))))
4 つの要素でテストを実行する:
=> (time (dotimes [x 1000000] (vector 1 2 3 4)))
"Elapsed time: 12.082104 msecs"
=> (time (dotimes [x 1000000] (vector-few 1 2 3 4)))
"Elapsed time: 443.056339 msecs"
=> (time (dotimes [x 1000000] (vector-many 1 2 3 4)))
"Elapsed time: 11.812106 msecs"
そして、5で:
=> (time (dotimes [x 1000000] (vector 1 2 3 4 5)))
"Elapsed time: 467.904979 msecs"
=> (time (dotimes [x 1000000] (vector-few 1 2 3 4 5)))
"Elapsed time: 537.080198 msecs"
=> (time (dotimes [x 1000000] (vector-many 1 2 3 4 5)))
"Elapsed time: 10.30695 msecs"
そして 8 の場合 (つまり、すべての関数が var-args ケースを使用しています):
=> (time (dotimes [x 1000000] (vector 1 2 3 4 5 6 7 8)))
"Elapsed time: 832.803266 msecs"
=> (time (dotimes [x 1000000] (vector-few 1 2 3 4 5 6 7 8)))
"Elapsed time: 689.526288 msecs"
=> (time (dotimes [x 1000000] (vector-many 1 2 3 4 5 6 7 8)))
"Elapsed time: 905.95839 msecs"