Skip to content

Instantly share code, notes, and snippets.

@johnroyer
Created June 3, 2024 04:23
Show Gist options
  • Save johnroyer/7e18c4dda88d14efe4e96b3c05d0ba9b to your computer and use it in GitHub Desktop.
Save johnroyer/7e18c4dda88d14efe4e96b3c05d0ba9b to your computer and use it in GitHub Desktop.
使用 os.Read() 讀取檔案遇到的讀取速度問題
package main
import (
"io"
"os"
)
func main() {
// 1 MB
bufferSize := 1 * 1024 * 1024
filePtr, _ := os.Open("source.img")
defer filePtr.Close()
buff := make([]byte, bufferSize)
for {
_, fileReadError := filePtr.Read(buff)
if io.EOF == fileReadError {
break
}
}
}
// source.img 為 1 GB 的檔案,存放在 SSD 上
//
//
// 若 bufferSize 設定為 1 MB,則讀取檔案需 144 ms
// 若 bufferSize 設定為 128 MB,則讀取檔案需 200 ms
// 若 bufferSize 設定為 1024 MB,則讀取檔案需 400 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment