- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在处理一个 csv 文件,其中最后一列的格式并不总是相同。每行都有这样的结构:
"Root/Word1","some string","some string","some œ0'fqw[唃#”≠§
\nfw@\tfa0j
"
"Root/Word2","some string","some string","some string"
...
所以 6 列和最后一个可以包含\n。这使得很难按组件拆分。另一个限制是所有字符串都可以是任何可能的特殊字符。这使得使用正则表达式变得困难。
我决定先用蛮力解决问题。 (是的,我已经看到索引偏移量为 O(n)。但无法提出替代方案。)
static func importData(_ db: DB) {
let csvString = readDataFromCSV(fileName: "data", fileType: "csv")!
let totalCharCount = csvString.count
print("total: \(totalCharCount)")
for i in 0..<totalCharCount {
print(i)
if i+5 >= totalCharCount {
continue
}
let index = csvString.index(csvString.startIndex, offsetBy: i)
let endIndex = csvString.index(csvString.startIndex, offsetBy:i+5)
let part = csvString[index ..< endIndex]
if part == "Root/" {
let accum = lookInside(i: i, totalCharCount: totalCharCount, csvString: csvString)
var rows = accum.components(separatedBy: "\",\"")
if var lastt = rows.last {
lastt.removeLast()
lastt.removeLast()
rows[rows.count-1] = lastt
}
}
}
}
static func lookInside(i:Int, totalCharCount: Int, csvString: String) -> String {
var accum = ""
var found = false
var j = i+5
while !found {
if j+5 >= totalCharCount {
found = true
}
let index2 = csvString.index(csvString.startIndex, offsetBy: j)
let endIndex2 = csvString.index(csvString.startIndex, offsetBy:j+5)
if csvString[index2 ..< endIndex2] == "Root/" {
found = true
accum.removeLast()
} else {
accum += String(csvString[index2])
}
j += 1
}
return accum
}
基本上,我正在遍历整个字符串以寻找模式“Root/”。找到后,我会从这一刻前进到模式的下一次出现。
问题是 csv 生成一个 200k 个字符长的字符串,当我在模拟器上运行它时它持续了太多时间(~30 分钟)。
所以现在我在这里寻求一些帮助,因为根据 Instruments,所有时间都在 String.index(offset by) 方法中消耗,该方法被调用了太多次。
最佳答案
Shlemiel gets a job as a street painter, painting the dotted lines down the middle of the road. On the first day he takes a can of paint out to the road and finishes 300 yards of the road. “That’s pretty good!” says his boss, “you’re a fast worker!” and pays him a kopeck.
The next day Shlemiel only gets 150 yards done. “Well, that’s not nearly as good as yesterday, but you’re still a fast worker. 150 yards is respectable,” and pays him a kopeck.
The next day Shlemiel paints 30 yards of the road. “Only 30!” shouts his boss. “That’s unacceptable! On the first day you did ten times that much work! What’s going on?”
“I can’t help it,” says Shlemiel. “Every day I get farther and farther away from the paint can!”
您需要了解字符串的工作原理。糟糕的字符串处理在 2001 年很慢,当时 Joel Spolsky 写了上面的故事。通过适当的 Unicode 处理,事情变得更加昂贵。
您并没有真正遍历字符串。您可以使用 String.startIndex
一次又一次地重新开始。
而是使用返回 String.Index
结果的函数。这是字符串的有效索引。用它来保存你的最后一个位置,然后相对于这个位置工作。如果您在开始时仅使用一次 String.Index
,您的代码会更快。
关于Swift 4 字符串索引在处理大字符串时偏移太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47336928/
我用 cudaMemcpy()一次将 1GB 的数据精确复制到设备。这需要 5.9 秒。反之则需要 5.1 秒。这是正常的吗?函数本身在复制之前有这么多开销吗? 理论上,PCIe 总线的吞吐量至少应为
我正在尝试读取图像的大小并在其高度大于 150 时调整其边距。但是在运行这段代码时,我总是在控制台中得到一个“0”: var coverImg; coverImg =
我正在开发一个 iPhone 应用程序,其中包含一些标准的“相机”功能。保存到相机胶卷真的太慢了,在iPhone 4上大约需要四秒钟。有什么办法可以提高速度吗? 如果您查看默认的 iPhone 相
我创建了一个将图像转换为灰度的类。但它的工作速度太慢了。有没有办法让它运行得更快? 这是我的类(class): @implementation PixelProcessing SYNTHESIZE_S
我使用以下代码,结果是正确的,但 gethostbyaddr 需要大约 30 秒。 function IPAddrToName(IPAddr: string): string; var So
我有以下案例, public class Test { private static final int MAX_NUMBER = 10_00_00; public static vo
我已经正确添加了所有必需的 JARS: Ucanaccess 3.0.4 commons-lang-2.6 commons-logging-1.1.1 hsqldbd jackcess-2.1.3 我
我为特定功能构建了一个多处理密码破解程序(使用单词列表),与使用单个进程相比,它减少了一半的时间。 最初的问题是,它会向您显示破解的密码并终止工作人员,但剩余的工作人员将继续工作,直到他们用完可哈希的
我在我的一个 JSP 中引入了 Sencha 网格。本地 sencha 相当快,但在外部服务器上它太慢了。 我在这里按照部署说明进行操作 http://docs.sencha.com/ext-js/4
我的查询加载时间有很大问题。在这种情况下,我需要 hg_ft_won 列(表:值)中的值,用于 home_team_id 和 away_team_id(表:匹配)。 它确实可以正常工作。加载只需要很长
我现在正在学习不同类型的排序,我发现,从某个点开始,我的快速排序算法根本无法快速工作。 这是我的代码: class QuickSort { // partitioning arr
为什么要模式 [0123]123456|98765 比在 Java 中执行 [0123]123456 然后 98765 慢两倍?所以单独搜索它们比用 OR 执行更快。有人有解释吗? UPD 查看带有结
我有带 Assets 的 Android 应用程序。它们包含 20,000 多个文件,其中大部分是简单的文本或 png 文件,分为不同的文件夹和子文件夹。1 个单个文件的最大大小为 500kb,其中
您好,我在查询中添加了 GROUP_CONCAT 函数,该函数终止了我的查询:/。我的查询是: SELECT u.username,a.user_id,a.id,a.text,a.lang as fr
我正在寻找优化查询的想法。 目前,我有一个 4M 行的表,我只想检索引用的最后 1000 行: SELECT * FROM customers_material_events WHERE refere
我在我的应用程序中使用 NSURLConnection,我在其中扫描条形码,通过 NSURLConnection 发送 XML,Java 服务向我发回 XML。我的问题是,使用 Wifi 时,响应时间
当我运行以下程序时,执行大约需要 7 到 8 分钟。我真的不确定我哪里弄错了,因为这个程序执行起来要花很多时间。 public class Test { public stat
我正在使用 NSFetchResultsController 从数据库中接收项目(有 80.000 个项目)。 这是我的谓词:@"(desc CONTAINS[cd] %@)", [any text]
我在 x_data 中有一个 3x2000 numpy 数组,在 y_data 中有一个 1x2000 numpy 数组,我将其传递给此函数 regress 以给我一条回归线。它工作正常。问题是我正在
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。所以我一开始的代码是 for (int y = 0; y (y, x); // read pixel (0,0)
我是一名优秀的程序员,十分优秀!