在大型数组 (1,000) 中查找 firstIndex 的最快方法 Swift
Fastest way to find firstIndex in a large array (1,000) Swift
有没有更快的方法来查找自定义数组中的索引 object?
我有一个数组,其计数约为 1,000。在每个 SectionIndex 中都有一个包含我所有属性的 MainIndex,每个 SectionIndex 大约有 15 个 MainIndex 部分。 SectionIndex 用于 TableView 部分标题,.data[MainIndex] 部分填充每个部分中的行。
在 运行 工具(时间分析)启动我的应用程序所用的 5 秒之后,(array.firstIndex) 行花费了 3.89 秒,无论如何我可以加快我的部分找到索引?
var array: [SectionIndex] = [SectionIndex]()
函数中的主要部分:
let title = "\(dateMonth), \(dateYear)"
if let offset = array.firstIndex(where: { [=12=].title == title })
{
array[offset].data.append(insert)
if let mins = array[offset].minutes {
array[offset].minutes = mins + timeToMinutes(minutes)
}
}
else
{
let insert2 = SectionIndex(index: array.count, title: title, date: date, data: [insert], minutes: timeToMinutes(minutes))
array.append(insert2)
}
章节索引
class SectionIndex {
let index: Int?
let title: String?
let date: Date?
var data: [MainIndex] = [MainIndex]()
var minutes: Int?
init(index: Int, title: String, date: Date, data: [MainIndex], minutes: Int)
{
self.index = index
self.title = title
self.date = date
self.data = data
self.minutes = minutes
}
}
我需要查找或添加索引的原因:
我的数据来自一年中的不同月份,可以追溯到大约 1,000 个月,只要首先找到月份和年份,它就会成为标题,下次当它已经存在时,我将数据附加到该部分。
Xcode 12.2 (Swift 5)
根据评论建议和Rob的词典建议,我做了如下修改,解决了我的问题,性能也提高了很多!
var currentIndex: Int = 0
var dic: [String: Int] = [:]
// Code below runs 15,000 times for each database entry
if let offset = dic[title]
{
array[offset].data.append(insert)
if let mins = array[offset].minutes { array[offset].minutes = mins + timeToMinutes(minutes) }
}
else
{
let insert2 = SectionIndex(index: currentIndex, title: title, date: date, data: [insert], minutes: timeToMinutes(minutes))
dic[title] = currentIndex
currentIndex += 1
array.append(insert2)
}
我还删除了主要部分中的选项,其中包含许多条目,这进一步提高了加载性能。以上结果:
Main Thread: 1.91s
Start: 1.77s
System Interface Initialisation: 1.29s
UIKit Initialisation: 933 ms
Launching Initial Frame Rendering: 1.64s
我对结果很满意,因为这是将 15,000 个条目加载到 1,000 个部分的最坏情况,但如果有人有任何进一步的建议和改进,请告诉我。
有没有更快的方法来查找自定义数组中的索引 object?
我有一个数组,其计数约为 1,000。在每个 SectionIndex 中都有一个包含我所有属性的 MainIndex,每个 SectionIndex 大约有 15 个 MainIndex 部分。 SectionIndex 用于 TableView 部分标题,.data[MainIndex] 部分填充每个部分中的行。
在 运行 工具(时间分析)启动我的应用程序所用的 5 秒之后,(array.firstIndex) 行花费了 3.89 秒,无论如何我可以加快我的部分找到索引?
var array: [SectionIndex] = [SectionIndex]()
函数中的主要部分:
let title = "\(dateMonth), \(dateYear)"
if let offset = array.firstIndex(where: { [=12=].title == title })
{
array[offset].data.append(insert)
if let mins = array[offset].minutes {
array[offset].minutes = mins + timeToMinutes(minutes)
}
}
else
{
let insert2 = SectionIndex(index: array.count, title: title, date: date, data: [insert], minutes: timeToMinutes(minutes))
array.append(insert2)
}
章节索引
class SectionIndex {
let index: Int?
let title: String?
let date: Date?
var data: [MainIndex] = [MainIndex]()
var minutes: Int?
init(index: Int, title: String, date: Date, data: [MainIndex], minutes: Int)
{
self.index = index
self.title = title
self.date = date
self.data = data
self.minutes = minutes
}
}
我需要查找或添加索引的原因:
我的数据来自一年中的不同月份,可以追溯到大约 1,000 个月,只要首先找到月份和年份,它就会成为标题,下次当它已经存在时,我将数据附加到该部分。
Xcode 12.2 (Swift 5)
根据评论建议和Rob的词典建议,我做了如下修改,解决了我的问题,性能也提高了很多!
var currentIndex: Int = 0
var dic: [String: Int] = [:]
// Code below runs 15,000 times for each database entry
if let offset = dic[title]
{
array[offset].data.append(insert)
if let mins = array[offset].minutes { array[offset].minutes = mins + timeToMinutes(minutes) }
}
else
{
let insert2 = SectionIndex(index: currentIndex, title: title, date: date, data: [insert], minutes: timeToMinutes(minutes))
dic[title] = currentIndex
currentIndex += 1
array.append(insert2)
}
我还删除了主要部分中的选项,其中包含许多条目,这进一步提高了加载性能。以上结果:
Main Thread: 1.91s
Start: 1.77s
System Interface Initialisation: 1.29s
UIKit Initialisation: 933 ms
Launching Initial Frame Rendering: 1.64s
我对结果很满意,因为这是将 15,000 个条目加载到 1,000 个部分的最坏情况,但如果有人有任何进一步的建议和改进,请告诉我。