Details

Type: Enhancement

Status: Closed

Priority: Major

Resolution: Completed

Affects Version/s: None

Fix Version/s: Release 1.9

Component/s: None

Labels:

Patch:Code and Test

Approval:Ok
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 repeated evaluation of k for any element.
A criterium test of the example above shows:
 Before: 206.017411 µs
 After: 126.532306 µs
Patch: clj99v3.patch
Attachments
Activity
Andy Fingerhut
made changes 
Field  Original Value  New Value 

Attachment  clj99minkeymaxkeyperformancev1.txt [ 11682 ] 
Andy Fingerhut
made changes 
Reporter  Andy Fingerhut [ jafingerhut ]  
Priority  Minor [ 4 ]  
Patch  Code and Test [ 10002 ] 
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 
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 
Approval  Triaged [ 10120 ] 
Michael Blume
made changes 
Attachment  clj99v3.patch [ 15257 ] 
Alex Miller
made changes 
Summary  GC Issue 95: maxkey and minkey evaluate k multiple times for arguments  maxkey and minkey evaluate k multiple times for arguments 
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: {code} (apply maxkey #(Math/tan %) (range 1000)) {code} *Approach:* Avoid duplication evaluation of k for any element. *Patch:* clj99v2.patch 
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 repeated evaluation of k for any element. A criterium test of the example above shows: * Before: 206.017411 µs * After: 126.532306 µs *Patch:* clj99v3.patch 
Approval  Triaged [ 10120 ]  Prescreened [ 10220 ] 
Alex Miller
made changes 
Priority  Minor [ 4 ]  Major [ 3 ] 
Rich Hickey
made changes 
Approval  Prescreened [ 10220 ]  Ok [ 10007 ] 
Alex Miller
made changes 
Fix Version/s  Release 1.9 [ 10750 ] 
Stuart Halloway
made changes 
Resolution  Completed [ 1 ]  
Status  Open [ 1 ]  Closed [ 6 ] 
Converted from http://www.assembla.com/spaces/clojure/tickets/99