How make a benchmark in a regular program? (no *_test.go)

polaris · 2017-08-17 01:00:20 · 430 次点击    
这是一个分享于 2017-08-17 01:00:20 的资源,其中的信息可能已经有所发展或是发生改变。

In C we have <time.h> and clock() and CLOCKS_PER_SEC, delivering consistent and reliable times, how to do this in Go?


评论:

mcouturier:

You can use the testing package as a regular package in a regular go file... this is an example:

https://golang.org/pkg/testing/#example_B_RunParallel

EDIT: There is a function func Benchmark(f func(b *B)) BenchmarkResult {} that gives you a BenchmarkResult without outputting it.

nasciiboy:

brilliant! promising and interesting, I'll prove it tomorrow

hell_0n_wheel:

Why are you discounting Testing.B?

It's like you're asking "How to print to screen? (no fmt.Print)"

nasciiboy:

b.Log....? No, it is to make comparisons of regexps with several languages and libraries (comming son ^-^ (again...)), and the output of test is far from being appropriate for that work

hell_0n_wheel:

the output of test is far from being appropriate for that work

No idea what you're on about. The output of test is perfect for that use case.

titpetric:

How about using the same:

//#include <time.h>
import "C"
import "time"
var startTime = time.Now()
var startTicks = C.clock()
func CpuUsagePercent() float64 {
    clockSeconds := float64(C.clock()-startTicks) / float64(C.CLOCKS_PER_SEC)
    realSeconds := time.Since(startTime).Seconds()
    return clockSeconds / realSeconds * 100
}

Source: stackoverflow comment

FUZxxl:

Oh no please don't. cgo has a rather large overhead, distorting your measurements.

titpetric:

Want to give a stab at implementing it in assembly, or would it not work (ie, improve the situation)? :)

FUZxxl:

No. Just use the Go API. Though I guess you could tr to read the performance counters in assembly.

nasciiboy:

EXCELSIOR!

package main
//#include <time.h>
import "C"
import (
  "fmt"
)
func main(){
  init := CpuTime()
  // ....
  fmt.Printf( "CpuTime %d\n", DiffCpuTimeByMS( init, CpuTime() ) )
}
func CpuTime() uint64 {
  return uint64(C.clock())
}
func DiffCpuTimeByMS( begin, end uint64 ) uint64 {
  return (end - begin) *  1000 / uint64(C.CLOCKS_PER_SEC)
}

compiling

$ go build main.go
SpokenSpruce:

Not sure if this is very precise, but this is how I did that in a test just to see how much time all of the subtests took.

startTime := time.Now()
// Do Stuff
result := time.Since(startTime)
nasciiboy:

time.Now(), returns system time, on the other hand, clock (C) returns an approximation of the processor time used by the program

vietnq:

You can use time API with Go 1.9 https://tip.golang.org/pkg/time/#hdr-Monotonic_Clocks

nasciiboy:

thanks for the info. I have updatedand and tested, althought the C clock() is more consistent. The official documetation... is a bit confusing


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

430 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传