I have the following code:
func numberOfRows(in tableView: NSTableView) -> Int {
switch tableView {
case self.stringsTable:
return self.stringsList?.count ?? 0
case self.localeTable:
return self.localeMap.count
default:
print("numberOfRows not handled for \(tableView)")
return 0
}
}
I wonder if there is any (performance) difference between case
and ====
operator like below:
func numberOfRows(in tableView: NSTableView) -> Int {
if tableView === self.stringsTable {
}
// ...
}
I did the following test (Xcode simulator), for a table declared as IBOutlet:
func numberOfSections(in tableView: UITableView) -> Int {
var start = CFAbsoluteTimeGetCurrent()
for _ in 0..<1000_000 {
if table == tableView { }
}
var diff = CFAbsoluteTimeGetCurrent() - start
print("==", diff)
start = CFAbsoluteTimeGetCurrent()
for _ in 0..<1000_000 {
if table === tableView { }
}
diff = CFAbsoluteTimeGetCurrent() - start
print("===", diff)
return 1
}
I get the following values:
== 0.7980579137802124
=== 0.647942066192627
I repeated the test with a tableView declared in code:
var testTableView: UITableView = UITableView()
func numberOfSections(in tableView: UITableView) -> Int {
var start = CFAbsoluteTimeGetCurrent()
for _ in 0..<1000_000 {
if table == tableView { }
}
var diff = CFAbsoluteTimeGetCurrent() - start
print("==", diff)
start = CFAbsoluteTimeGetCurrent()
for _ in 0..<1000_000 {
if table === tableView { }
}
diff = CFAbsoluteTimeGetCurrent() - start
print("===", diff)
start = CFAbsoluteTimeGetCurrent()
for _ in 0..<1000_000 {
if testTableView == tableView { }
}
diff = CFAbsoluteTimeGetCurrent() - start
print("== testTableView", diff)
start = CFAbsoluteTimeGetCurrent()
for _ in 0..<1000_000 {
if testTableView === tableView { }
}
diff = CFAbsoluteTimeGetCurrent() - start
print("=== testTableView", diff)
return 1
}
This time, may be because testTableView is empty, === was faster:
== 0.7994720935821533
=== 0.6421390771865845
== testTableView 0.5936309099197388
=== testTableView 0.6272460222244263
In any case, difference is about 1/10 of a millionth…