GolangNote

Golang笔记

go 与python 遍历目录性能对比

Permalink

相对 python 来说快1/3,用 python 的 Walk 来遍历需要30分钟,go Walk 用17分钟。

go 与python 遍历目录性能对比

Go: Walk 遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func scanFile(rootPath string) map[string]string {
  fileMD5 := make(map[string]string)
  rootPrefix := strings.TrimSuffix(rootPath, "/")
  filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
    // fmt.Println("Processing:", path)
    //这里是文件过滤器
    if strings.HasSuffix(path, ".php") {
      //fmt.Println(path)
      furi := strings.TrimPrefix(path, rootPrefix)
      file, inerr := os.Open(path)
      if inerr == nil {
        md5h := md5.New()
        io.Copy(md5h, file)
        md5v := fmt.Sprintf("%x", md5h.Sum(nil))
        //fmt.Println(furi, md5v)
        fileMD5[furi] = md5v
      }
    }
    return err
  })

  return fileMD5
}

Related articles

Write a Comment to "go 与python 遍历目录性能对比"

Submit Comment Login
Based on Golang + fastHTTP + youdb | go1.16 Processed in 1ms