Uploaded image for project: 'Swift'
  1. Swift
  2. SR-1577

Ternary operator much slower to compile than if / else

    XMLWordPrintable

    Details

      Description

      A function with this line took about 4 seconds to compile in debug.

      let sorted = picks.sort { $0.pick_line == $1.pick_line ? $0.position < $1.position : $0.pick_line < $1.pick_line }
      

      This equivalent version took only 5.4ms:

          let sorted = picks.sort {
              if $0.pick_line == $1.pick_line {
                  return $0.position < $1.position
              } else {
                  return $0.pick_line < $1.pick_line
              }
          }
      

      picks is an array of instances of an NSManagedObject subclass, position and pick_line are both optional Integer types. (I don't know if this is relevant).

      Not sure if this should be labelled "Performance". Is that for issues in the compilation time or the performance of the compiled code.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              josephlord Joseph Lord
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated: