Created
June 3, 2024 04:23
-
-
Save johnroyer/7e18c4dda88d14efe4e96b3c05d0ba9b to your computer and use it in GitHub Desktop.
使用 os.Read() 讀取檔案遇到的讀取速度問題
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
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