A Brief discussion on the comparison of SEVERAL ways of GoLang reading documents

  • 2020-06-23 00:35:12
  • OfStack

GoLang provides a number of ways to read files. Generally there are three common ways to read files. Use Read plus buffer, and use the bufio and ioutil libraries.

How efficient are they? Use a simple program to evaluate 1:


package main 
 
import( 
  "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,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,err := 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 to specify the files to read. Here I compare the log files of 13.7MB. The time consumed by the three methods is as follows:


Cost time 105.006ms 
Cost time 68.0039ms 
Cost time 31.0018ms 

Read 29.3MB's media files:


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, as ioutil's approach is highly efficient. If you have time, you can analyze 1 more from the code level.


Related articles: