Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Comparison of several ways of reading files in GoLang

Shulou Source: shulou.com Published: 2022-06-01 02:47:13 10月04日 Update

GoLang provides many ways to read files, generally speaking, there are three commonly used. Use Read plus buffer, use the bufio library and the ioutil library.

What about their efficiency? Use a simple program to evaluate:

Package mainimport ("fmt"os"flag"io"io/ioutil"bufio"time") func read1 (path string) string {fi,err: = os.Open (path) if err! = nil {panic (err)} defer fi.Close () chunks: = make ([] byte,1024,1024) buf: = make ([] byte,1024) for {n Err: = fi.Read (buf) if err! = nil & & err! = io.EOF {panic (err)} if 0 = = n {break} chunks=append (chunks,buf [: n]...) / / fmt.Println (string (buf [: n]))} return string (chunks)} func read2 (path string) string {fi Err: = os.Open (path) if err! = nil {panic (err)} defer fi.Close () r: = bufio.NewReader (fi) chunks: = make ([] byte,1024,1024) buf: = make ([] byte,1024) for {n make err: = r.Read (buf) if err! = nil & & err! = io.EOF {panic (err)} if 0 = = n {break} chunks=append (chunks) Buf [: n]...) / / fmt.Println (string (buf [: n])} return string (chunks)} func read3 (path string) string {fi,err: = os.Open (path) if err! = nil {panic (err)} defer fi.Close () fd Err: = ioutil.ReadAll (fi) / / fmt.Println (string (fd)) return string (fd)} func main () {flag.Parse () file: = flag.Arg (0) f file: = ioutil.ReadFile (file) if err! = nil {fmt.Printf ("% s\ n") Err) panic (err)} fmt.Println (string (f)) start: = time.Now () read1 (file) T1: = time.Now () fmt.Printf ("Cost time% v\ n", t1.Sub (start)) read2 (file) T2: = time.Now () fmt.Printf ("Cost time% v\ n" T2.Sub (T1) read3 (file) T3: = time.Now () fmt.Printf ("Cost time% v\ n", t3.Sub (T2))}

Run the command go run read.go filename and specify the files you need to read. Here I compare reading the log files of 13.7MB. The time consumed by the three methods is:

Cost time 105.006ms

Cost time 68.0039ms

Cost time 31.0018ms

Read the media file for 29.3MB:

Cost time 390.0223ms

Cost time 194.0111ms

Cost time 83.0048ms

Read the media file of 302MB

Cost time 40.8043338s

Cost time 1m5.0407201s

Cost time 8.8155043s

This gap is obvious, and the methods provided by ioutil are efficient. You can analyze it again at the code level when you have time.

Tags: Files methods media media files efficiency obviously generally speaking code commands that is levels gaps commonly used methods logs time programs analysis consumption evaluation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS Huawei Redmi OPPO Reno