-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
222 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ struct HomeView: View { | |
.onOpenURL(perform: { url in | ||
// 处理外部链接 | ||
}) | ||
|
||
#endif | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// BadCase.swift | ||
// SwiftPamphletApp | ||
// | ||
// Created by Ming on 2024/11/13. | ||
// | ||
|
||
import Foundation | ||
|
||
struct TaskCase { | ||
static func bad() { | ||
TaskCase.badLoadFile() // 读取文件 | ||
TaskCase.badSemaphore() // 信号量 | ||
TaskCase.badJSONDecode() // JSON 解析 | ||
} | ||
|
||
static func good() { | ||
TaskCase.goodLoadFile() // 读取文件 | ||
TaskCase.goodSemaphore() // 信号量 | ||
TaskCase.goodJSONDecode() // JSON 解析 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// TaskCaseJSON.swift | ||
// SwiftPamphletApp | ||
// | ||
// Created by Ming on 2024/11/14. | ||
// | ||
|
||
import Foundation | ||
|
||
// 数据模型 | ||
struct TCItem: Codable, Identifiable { | ||
let id: Int | ||
let title: String | ||
let description: String | ||
} | ||
|
||
extension TaskCase { | ||
static func badJSONDecode() { | ||
let jsonData = TaskCase.generateLargeJSON() | ||
do { | ||
_ = try JSONDecoder().decode([TCItem].self, from: jsonData) | ||
|
||
} catch { | ||
print("解析失败: \(error)") | ||
} | ||
Perf.showTime("未优化JSON解析") | ||
} | ||
|
||
static func goodJSONDecode() { | ||
Task.detached(priority: .background) { | ||
do { | ||
_ = try await parseJSON() | ||
Perf.showTime("异步优化JSON解析") | ||
} catch { | ||
print("解析失败: \(error)") | ||
} | ||
} | ||
} | ||
|
||
// 异步解析JSON | ||
@Sendable | ||
static func parseJSON() async throws -> [TCItem] { | ||
let jsonData = TaskCase.generateLargeJSON() | ||
return try JSONDecoder().decode([TCItem].self, from: jsonData) | ||
} | ||
|
||
static func generateLargeJSON() -> Data { | ||
var items: [[String: Any]] = [] | ||
for i in 0...10000 { | ||
items.append([ | ||
"id": i, | ||
"title": "标题 \(i)", | ||
"description": "这是一段很长的描述文本,用来模拟实际场景中的数据量 \(i)" | ||
]) | ||
} | ||
return try! JSONSerialization.data(withJSONObject: items) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
SwiftPamphletApp/Performance/TaskCase/TaskCaseLoadFile.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// TaskCaseLoadFile.swift | ||
// SwiftPamphletApp | ||
// | ||
// Created by Ming on 2024/11/14. | ||
// | ||
|
||
import Foundation | ||
|
||
extension TaskCase { | ||
// 同步读取方式 - 会阻塞主线程 | ||
static func badLoadFile() { | ||
// 模拟耗时操作,减少循环次数并添加延迟 | ||
var content = "" | ||
for i in 1...10 { | ||
content += "这是第\(i)行内容\n" | ||
Thread.sleep(forTimeInterval: 0.3) // 每次循环暂停0.3秒 | ||
} | ||
Perf.showTime("未优化文件读取") | ||
} | ||
|
||
// 异步读取方式 - 推荐使用 | ||
static func goodLoadFile() { | ||
Task { | ||
do { | ||
_ = try await withCheckedThrowingContinuation { continuation in | ||
DispatchQueue.global().async { | ||
var content = "" | ||
for i in 1...10 { | ||
content += "这是第\(i)行内容\n" | ||
Thread.sleep(forTimeInterval: 0.3) // 每次循环暂停0.3秒 | ||
} | ||
continuation.resume(returning: content) | ||
} | ||
} | ||
|
||
// 更新UI要在主线程 | ||
await MainActor.run { | ||
Perf.showTime("异步优化文件读取") | ||
} | ||
} catch { | ||
await MainActor.run { | ||
print("读取文件失败") | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.