Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Comparison of several ways of reading files in GoLang

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report