Clojure

max-key and min-key evaluate k multiple times for arguments

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test
  • Approval:
    Prescreened

Description

max-key or min-key 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 max-key #(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: clj-99-v3.patch

  1. clj-99-v1.diff
    22/Oct/13 7:54 PM
    3 kB
    Andy Fingerhut
  2. clj-99-v2.patch
    09/Feb/15 2:50 PM
    3 kB
    Michael Blume
  3. clj-99-v3.patch
    03/Nov/15 7:52 PM
    3 kB
    Michael Blume

Activity

People

Vote (3)
Watch (3)

Dates

  • Created:
    Updated: