Exec.Command(...) execution time

Hello gophers, I’m currently seeing a strange behavior of the exec.Command(…) call of the os/exec package: it’s taking too much time!

The command that I’m executing normally (run directly from the Bash) runs in 0,01s but from the exec.Command(…) call it runs in 0,15s (tested with benchmark tests). Do you know why ?

I’m running go 1.5.1.

Can you provide your code and/or a minimal excerpt that demonstrates this?

What is the memory usage of your Go process?

1 Like

Of course I can.

func BenchmarkParseName(b *b.Testing) {
	cmd := exec.Command("python", "nameparser.py", "A.B.E.La.D.C.1961.ENG.AC3")
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return b.Error(err)
    m := MyIntf{}
    err = json.Unmarshal([]byte(strings.Replace(out.String(), "'", "\"", -1)), &m)
	if err != nil {
		return b.Error(err

➜ git:(master) ✗ go test -test.run="^BenchmarkParseName$"
ok 0.14s

➜ git:(master) ✗ time python nameparser.py "A.B.E.La.D.C.1961.ENG.AC3"
python nameparser.py 0,02s user 0,01s system 6% cpu 0,418 total
➜ git:(master) ✗

You may think that the problem is the unmarshal of the object or the strings.Replace() but even by commenting those lines, I got the same result.

The python script that I’m running is [this][1].
P.S. I know that I can translate the PTN python library that I’m using in Go, and that’s exactly what I’m doing but this is not the subject of this post.

Sorry for mistakes in the code and text but I’m currently writing this from a tablet.
[1]: https://github.com/divijbindlish/parse-torrent-name

There are three issues with your benchmarking approach:

  1. go test creates a test program and compiles it before running the actual test. To measure just execution time of a test you should compile the test program separately:

     go test -c
     time ./testprogname.test
  2. Your BenchmarkParseName function is not a real benchmark function. You need to enclose its body in the following loop:

     for i := 0; i < b.N; i++ {
  3. Running go test -test.run="^BenchmarkParseName$" doesn’t run any tests or benchmarks. The correct command is

     go test -test.bench="^BenchmarkParseName$"

I think you will find the following article useful: http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.