Details

Type: Enhancement

Status: Open

Priority: Minor

Resolution: Unresolved

Affects Version/s: None

Fix Version/s: None

Component/s: None

Labels:

Patch:Code and Test

Approval:Triaged
Description
maxkey or minkey will evaluate (k value) multiple times for arguments if more than 2 arguments are passed. This is undesirable if k is expensive to calculate.
Good perf test:
(apply maxkey #(Math/tan %) (range 1000))
Approach: Avoid duplication evaluation of k for any element.
Patch: clj99v2.patch
Attachments
Activity
Andy Fingerhut
made changes 
Field  Original Value  New Value 

Attachment  clj99minkeymaxkeyperformancev1.txt [ 11682 ] 
Andy Fingerhut
made changes 
Patch  Code and Test [ 10002 ]  
Priority  Minor [ 4 ]  
Reporter  Andy Fingerhut [ jafingerhut ] 
Alex Miller
made changes 
Fix Version/s  Backlog [ 10035 ] 
Andy Fingerhut
made changes 
Attachment  clj99v1.diff [ 12375 ] 
Andy Fingerhut
made changes 
Attachment  clj99minkeymaxkeyperformancev1.txt [ 11682 ] 
Alex Miller
made changes 
Labels  performance 
Michael Blume
made changes 
Attachment  clj99v2.patch [ 13857 ] 
Alex Miller
made changes 
Approval  Triaged [ 10120 ]  
Description 
{code}
Reported by H.Duerer, Mar 13, 2009 maxkey or minkey will evaluate (k value) multiple times for arguments if more than 2 arguments are passed. This is undesirable if k is expensive to calculate. Something like the code below would avoid these double calculations (at the price of generating more ephemeral garbage) (defn maxkey "Returns the x for which (k x), a number, is greatest." ([k x] x) ([k x y] (if (> (k x) (k y)) x y)) ([k x y & more] (second (reduce (fn [x y] (if (> (first x) (first y)) x y)) (map #(vector (k %) %) (cons x (cons y more))))))) {code} 
maxkey or minkey will evaluate (k value) multiple times for arguments if more than 2 arguments are passed. This is undesirable if k is expensive to calculate.
Good perf test: {code} (apply maxkey #(Math/tan %) (range 1000)) {code} *Approach:* Avoid duplication evaluation of k for any element. *Patch:* clj99v2.patch 
Converted from http://www.assembla.com/spaces/clojure/tickets/99