Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- user> (use 'criterium.core)
- nil
- user> (defrecord Foo [a b])
- user.Foo
- user> (def r (Foo. 1 2))
- #'user/r
- user> (:a r)
- 1
- user> (:a r 3)
- 1
- user> (bench (:a r))
- Evaluation count : 5918775180 in 60 samples of 98646253 calls.
- Execution time mean : 8.016731 ns
- ...
- user> (bench (:a r 3))
- Execution time mean : 32.933088 ns ;; <= 4X higher than with the not-found arg!
- ...
- user> (.valAt ^clojure.lang.ILookup r :a)
- 1
- user> (.valAt ^clojure.lang.ILookup r :a 3)
- 1
- user> (bench (.valAt ^clojure.lang.ILookup r :a))
- Execution time mean : 7.979451 ns
- ...
- user> (bench (.valAt ^clojure.lang.ILookup r :a 3))
- Execution time mean : 7.368049 ns ;; <= No performance difference here
- ...
- ;; And yet this is the implementation of Keyword.invoke:
- final public Object invoke(Object obj) {
- if(obj instanceof ILookup)
- return ((ILookup)obj).valAt(this);
- return RT.get(obj, this);
- }
- final public Object invoke(Object obj, Object notFound) {
- if(obj instanceof ILookup)
- return ((ILookup)obj).valAt(this,notFound);
- return RT.get(obj, this, notFound);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement