讓你的轉換作業飛速運行
真實場景:當你需要轉換數百份文件時,每份省 1 秒就能省下好幾分鐘!
檔案越大,處理時間越長
PDF > DOCX > HTML > TXT
圖片、表格、程式碼區塊都會增加處理時間
CPU、記憶體、硬碟速度都有影響
在優化之前,先學會測量
# 方法一:使用 Measure-Command
Measure-Command {
pandoc input.md -o output.docx
}
# 輸出會顯示:
# TotalSeconds : 2.3456789
小技巧:多測幾次取平均值,結果更準確!
1開啟 Antigravity
2請 AI 幫你寫計時腳本
請幫我寫一個 PowerShell 腳本,
可以測量 Pandoc 轉換的執行時間,
並將結果輸出成報告
# benchmark.ps1
param(
[string]$InputFile,
[string]$OutputFormat = "docx"
)
$outputFile = [System.IO.Path]::ChangeExtension($InputFile, $OutputFormat)
Write-Host "開始測試: $InputFile" -ForegroundColor Cyan
$result = Measure-Command {
pandoc $InputFile -o $outputFile
}
Write-Host "轉換完成!" -ForegroundColor Green
Write-Host "耗時: $($result.TotalSeconds.ToString('F2')) 秒" -ForegroundColor Yellow
# 計算檔案大小
$inputSize = (Get-Item $InputFile).Length / 1KB
$outputSize = (Get-Item $outputFile).Length / 1KB
Write-Host "輸入: $($inputSize.ToString('F1')) KB"
Write-Host "輸出: $($outputSize.ToString('F1')) KB"
當 Markdown 檔案超過 1MB,轉換速度會明顯變慢
請幫我把這份很長的 Markdown 文件,
按照 ## 標題分割成多個小檔案,
每個檔案一個章節
效果:10MB 大檔分成 10 個 1MB 小檔,可以平行處理,總時間大幅縮短!
# split-markdown.ps1
param([string]$InputFile)
$content = Get-Content $InputFile -Raw -Encoding UTF8
$chapters = $content -split '(?=^## )'
$index = 1
foreach ($chapter in $chapters) {
if ($chapter.Trim()) {
$fileName = "chapter-$($index.ToString('D2')).md"
$chapter | Out-File $fileName -Encoding UTF8
Write-Host "建立: $fileName"
$index++
}
}
Write-Host "共分割成 $($index - 1) 個檔案"
# 轉換所有章節
Get-ChildItem chapter-*.md | ForEach-Object {
pandoc $_.FullName -o ($_.BaseName + ".html")
}
# 合併成一個完整 HTML
Get-Content chapter-*.html | Out-File complete.html -Encoding UTF8
同時轉換多個檔案,大幅提升效率
# parallel-convert.ps1
# PowerShell 7+ 支援平行處理
$files = Get-ChildItem *.md
$files | ForEach-Object -Parallel {
$output = $_.BaseName + ".docx"
pandoc $_.FullName -o $output
Write-Host "完成: $output"
} -ThrottleLimit 4 # 同時最多 4 個任務
ThrottleLimit:根據你的 CPU 核心數調整,建議設為核心數的一半
轉換 20 個 Markdown 檔案的時間比較
| 方法 | 耗時 | 說明 |
|---|---|---|
| 逐一轉換 | 60 秒 | 每個 3 秒 × 20 個 |
| 平行 x2 | 30 秒 | 同時 2 個任務 |
| 平行 x4 | 15 秒 | 同時 4 個任務 |
結論:平行處理可提升 2-4 倍效能!
# 只處理比輸出新的來源檔
Get-ChildItem *.md | ForEach-Object {
$output = $_.BaseName + ".docx"
# 檢查輸出是否存在且較新
if (!(Test-Path $output) -or
$_.LastWriteTime -gt (Get-Item $output).LastWriteTime) {
pandoc $_.FullName -o $output
Write-Host "轉換: $($_.Name)" -ForegroundColor Green
} else {
Write-Host "跳過: $($_.Name) (已是最新)" -ForegroundColor Gray
}
}
保存轉換結果,下次直接使用
# 使用檔案雜湊作為快取識別
$hash = Get-FileHash input.md -Algorithm MD5
$cachePath = ".cache/$($hash.Hash).docx"
| 選項 | 效果 | 節省時間 |
|---|---|---|
| --no-highlight | 停用程式碼高亮 | 約 10% |
| 不含圖片 | 跳過圖片處理 | 約 30% |
| 簡單格式 | 用 HTML 取代 PDF | 約 50% |
建議:草稿階段用簡單格式,定稿再用完整格式
# 使用串流方式處理,避免載入整個檔案到記憶體
Get-Content large-file.md -ReadCount 1000 | ForEach-Object {
# 分批處理
$_ | Out-File temp-chunk.md -Append
}
注意:如果電腦記憶體不足 8GB,處理大於 50MB 的檔案可能會很慢
我的批次轉換腳本跑很慢,
每轉換一個檔案需要 5 秒,
共有 100 個檔案,總共要 8 分鐘
請幫我分析可能的瓶頸,
並提供優化建議
AI 會幫你:分析問題、提供多種優化方案、改寫腳本
1找一個有 10 個以上 .md 檔案的資料夾
2用 Measure-Command 測量逐一轉換的時間
3改用平行處理,再測一次
4比較兩種方法的差異
# performance-test.ps1
param([string]$Folder = ".")
$files = Get-ChildItem "$Folder\*.md"
Write-Host "找到 $($files.Count) 個檔案" -ForegroundColor Cyan
# 測試逐一處理
$sequential = Measure-Command {
$files | ForEach-Object {
pandoc $_.FullName -o "$Folder\seq-$($_.BaseName).docx"
}
}
# 測試平行處理
$parallel = Measure-Command {
$files | ForEach-Object -Parallel {
pandoc $_.FullName -o "$using:Folder\par-$($_.BaseName).docx"
} -ThrottleLimit 4
}
Write-Host "`n效能比較:" -ForegroundColor Yellow
Write-Host "逐一處理: $($sequential.TotalSeconds.ToString('F1')) 秒"
Write-Host "平行處理: $($parallel.TotalSeconds.ToString('F1')) 秒"
Write-Host "提升: $(($sequential.TotalSeconds / $parallel.TotalSeconds).ToString('F1'))x"
記住:過早優化是萬惡之源,先確保程式正確,再來優化速度!
哪個選項最能提升批次轉換的效能?
下一步:在 Unit 02 學習錯誤處理與除錯技巧