Set intersect and exclusiveOr methods performance decrease

Originator:konstantin.koval1
Number:rdar://22362776 Date Originated:20-Aug-2015 06:55 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (7A176x)
Classification:Performance Reproducible:Always
 
Summary:
Set  intersect and exclusiveOr methods performance has decreased in Xcode7.0 beta 5 (7A176x) with comparison to
 Xcode 6.4 (6E35b).

500_000 elements

Xcode 6.4 (6E35b) :
Interset 		- 0.06 sec
ExclusiveOr	- 0.12 sec

Xcode7.0 beta 5 (7A176x):
Interset 		- 1.16 sec
ExclusiveOr	- 1.22 sec

Steps to Reproduce:
Create 2 random sets and use intersect  and exclusiveOr methods

Code Example: 
func set_intersect(set: Set<Int>) {
  
  let otherSet = makeRandomSet(set.count)
  
  var res: Set<Int> = Set()
  measure("Intersect") {
    res = set.intersect(otherSet)
  }
  print(res.count)
}

func set_exclusiveOr(set: Set<Int>) {
  
  let otherSet = makeRandomSet(set.count)
  
  var res: Set<Int> = Set()
  measure("ExclusiveOr") {
    res = set.exclusiveOr(otherSet)
  }
  print(res.count)
}

func makeRandomSet(count: Int) -> Set<Int> {
  
  var set = Set<Int>(minimumCapacity: count)
  for _ in 0...count  {
    set.insert(Int(arc4random()))
  }
  return set
}


func measure(_ title: String? = nil, times: Int = 1, call: () -> Void) {
  var results = [CFTimeInterval]()
  for _ in 1...times {
    let startTime = CACurrentMediaTime()
    call()
    let endTime = CACurrentMediaTime()
    let time =  endTime - startTime
    results.append(time)
  }
  
  title.map { print("\($0) : ", appendNewline: false) }
  let avarage = results.reduce(0, combine: +) / Double(times)
  print("() Average - \(avarage)")
}

// Run test 

let set = makeRandomSet(5_000_000)

set_exclusiveOr(set)
set_intersect(set)

Expected Results:
performance should be the same of better.

Actual Results:
Performance is worse

Notes:
Examples project is attached.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!