Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5337

Collection.unique() is O(n^2) time -- suggestion for improvement

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 1.8.5
    • None
    • groovy-jdk
    • ubuntu 10

    Description

      I tried Collection.unique

      {closure}

      and it was getting very slow when the dataset grew.
      I believe it to be O(n^2)

      I have not yet looked it up in the groovy sources,
      but I believe that it must be implemented,
      like calling that closure over and over again.

      I'd like to suggest an alternative implementation,
      which actually made things toward O in my case.

      like:
      Map unique
      unique.put(closure(it), it)
      return unique.values()

      While this might use some more memory,
      it really beats the current implementation hands of.

      Please note that in this type of implementation,
      the closure is only called once per element,
      and all the burden of uniquing is placed on the map.

      It should be consistent with the contract of the current api.
      But it's much faster!

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              eike eike
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 24h
                  24h
                  Remaining:
                  Remaining Estimate - 24h
                  24h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified