在Go语言中,为什么”File.Readdirnames”会进行”clock_gettime”系统调用?

huangapple go评论101阅读模式
英文:

In Go, why does "File.Readdirnames" make a "clock_gettime" system call?

问题

作为对这个问题的后续,我正在尝试编写一个Go程序,以高效地列出文件名,而不进行不必要的系统调用。以下是我目前的代码:

package main

import (
        "os"
        "fmt"
        "log"
)

func main() {

        // 打开目录并检查错误
        f, err := os.Open(".")
        if err != nil {
                log.Fatal(err)
        }

        // 获取文件名
        files, err := f.Readdirnames(0)
        if err != nil {
                log.Fatal(err)
        }

        // 打印文件名
        fmt.Print(files, "\n")
}

然而,当我运行strace时,我看到很多类似以下的输出:

clock_gettime(CLOCK_REALTIME, {1406822401, 824793686}) = 0

这是什么意思?我如何使这段代码更高效?

英文:

As a follow up to this question, I am trying to write a Go program that only lists a files name in an efficient matter without unnecessary system calls. This is what I have thus far:

package main

import (
        "os"
        "fmt"
        "log"
)

func main() {

        // Open directory and check for errors
        f, err := os.Open(".")
        if err != nil {
                log.Fatal(err)
        }

        // Get file names
        files, err := f.Readdirnames(0)
        if err != nil {
                log.Fatal(err)
        }

        // Print files
        fmt.Print(files, "\n")
}

However, when I run an strace, I see many of the following:

clock_gettime(CLOCK_REALTIME, {1406822401, 824793686}) = 0

What does that pertain to? How can I make this code more efficient?

答案1

得分: 0

我正在翻译以下内容:

我只是随便猜测,但我认为这与Go的内置调度器和垃圾回收有关。

简而言之,由于Go提供了所有额外的运行时功能,它永远不会像C语言那样快。

你列出的代码可能是在Go中实现你想要的功能的最快方法。

英文:

I am spitballing, but I would think that it is related to Go's built-in scheduler and garbage collection.

Short answer is, go will never be quite as fast as C because it provides all of that extra runtime functionality which C doesn't.

The code you listed is probably the fastest way to do what you want in Go.

答案2

得分: -2

clock_gettime只是一个系统调用,在Go的运行时中被调用。运行时会做很多事情,比如调度等,所以有这么多的clock_gettime是正常的。

我认为你不能让这个程序更高效,因为它只是一个非常小的函数。你不应该在意这个。

英文:

clock_gettime is just a system call,which is called in go's runtime. runtime do a lot of things, schedual, and so on, so it's normal for so many clock_gettime.

I don't think you can make this program more efficient, 'cause it is just for so tiny function .You shouldn't mind it.

huangapple
  • 本文由 发表于 2014年8月1日 00:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/25063756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定