There's no substitute for measurement.

(defn maptest [f]
  (let [m (apply hash-map (range 40))]
    (time (dotimes [_ 100000] (f m)))
    (f m)))

(maptest #(zipmap (keys %) (map inc (vals %))))
; "Elapsed time: 1405.890766 msecs"
;=> {0 2, 32 34, 2 4, 34 36, 4 6, 36 38, 6 8, 38 40, 8 10, 10 12, 12 14, 14
16, 16 18, 18 20, 20 22, 22 24, 24 26, 26 28, 28 30, 30 32}

(maptest #(into {} (map (fn [[k v]] [k (inc v)]) %)))
; "Elapsed time: 1433.270362 msecs"
;=> {0 2, 32 34, 2 4, 34 36, 4 6, 36 38, 6 8, 38 40, 8 10, 10 12, 12 14, 14
16, 16 18, 18 20, 20 22, 22 24, 24 26, 26 28, 28 30, 30 32}

(maptest #(reduce-kv (fn [m k v] (assoc m k (inc v))) {} %))
; "Elapsed time: 649.452695 msecs"
;=> {0 2, 32 34, 2 4, 34 36, 4 6, 36 38, 6 8, 38 40, 8 10, 10 12, 12 14, 14
16, 16 18, 18 20, 20 22, 22 24, 24 26, 26 28, 28 30, 30 32}

These results are fairly robust in relation to each other for other sizes
of smallish maps.

--Chouser


On Sat, Feb 1, 2014 at 10:35 PM, Justin Smith <noisesm...@gmail.com> wrote:

> Realistically, how many situations are there where running keys and vals
> independently is preferable to running seq once and using the two element
> vectors that returns?
>
> Usually this way one can avoid walking the whole thing twice.
>
> (into {} (map (fn [[k v]] [k (inc v)]) {:a 0 :b 1})) is representative of
> the idiom I usually use. As a bonus, into uses transients, which can create
> the resulting structure in fewer cycles / less time.
>
>
> On Saturday, February 1, 2014 10:00:33 AM UTC-8, Sam Ritchie wrote:
>
>> Looks like Rich just chimed in with:
>>
>> "keys order == vals order == seq order "
>>
>>   Matching Socks
>>  January 31, 2014 7:31 PM
>> Actually, http://dev.clojure.org/jira/browse/CLJ-1302 "keys and vals
>> consistency not mentioned in docstring" was declined, with the comment "The
>> absence of this property in the docs is correct. You should not rely on
>> this."
>>
>>
>>
>> On Wednesday, August 11, 2010 6:03:39 PM UTC-4, Chouser wrote:
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@googlegroups.com
>>
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+u...@googlegroups.com
>>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+u...@googlegroups.com.
>>
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>> --
>> Sam Ritchie (@sritchie)
>> Paddleguru Co-Founder
>> 703.863.8561
>> www.paddleguru.com
>> Twitter <http://twitter.com/paddleguru> // 
>> Facebook<http://facebook.com/paddleguru>
>>
>


-- 
--Chouser

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to