GolangNote

Golang笔记

go 与python 遍历目录性能对比

Permalink

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

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
}

本文网址: https://golangnote.com/topic/97.html 转摘请注明来源

Related articles

Golang Range 的性能提升Tip

Go 语言里使用 range 可以方便遍历数组(array)、切片(slice)、字典(map)和信道(chan)。这里主要关注他们的性能。...

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

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 3ms