2

これは機能しますが、名前空間を動的に渡し、ns-resolveを使用してそれを解決する場合のパフォーマンスの低下について知りたいです...

(ns bulbs.vertices)

(defn create
  [config data]
  ((ns-resolve (:ns config) 'create-vertex) config data))

そして、それをこのように呼びます...

(ns bulbs.neo4jserver.graph
  (:require [bulbs.vertices :as vertices])
  (:require [bulbs.neo4jserver.client :as client]))

(defn graph
  [& [config]]
  (let [config (client/build-config config {:ns 'bulbs.neo4jserver.client})]
    (fn [func & args]
      (apply func config args))))

(def g (graph))

(g vertices/create {:name "James"})
4

1 に答える 1

4

あなたns-resolveがループの一部でない限り(そして、反復ごとに異なる関数を動的に解決する必要がない限り、これはまったく必要ありません)、パフォーマンスの低下について心配する必要はありません。

しかし、はい、パフォーマンスのペナルティがあります。

user=> (time (dotimes [n 1000000] (let [f (ns-resolve 'clojure.core 'inc)] (f n))))
"Elapsed time: 175.386 msecs"
nil
user=> (time (dotimes [n 1000000] (let [f inc] (f n))))
"Elapsed time: 27.022 msecs"
nil

本当に本当に魔法がns-resolve必要な場合(ただし、特定の状況でこれを行うことを思いとどまらせる他の質問への回答を参照してください)解決しようとしている関数がループで使用されている場合は、そのループから解決を取り出します。

user=> (time (let [f (ns-resolve 'clojure.core 'inc)] (dotimes [n 1000000] (f n))))
"Elapsed time: 48.538 msecs"
nil
于 2012-05-13T23:32:38.457 に答える