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 泛型性能初识

编程时,我们通常需要编写“泛型”函数,其中确切的数据类型并不重要。例如,您可能想编写一个简单的函数来对数字进行求和。Go 直到最近才有这个概念,但最近才添加了它(从1.18版开始)。...

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

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