<p>In C we have <time.h> and <code>clock()</code> and <code>CLOCKS_PER_SEC</code>, delivering consistent and reliable times, how to do this in Go?</p>
<hr/>**评论:**<br/><br/>mcouturier: <pre><p>You can use the <a href="https://golang.org/pkg/testing/" rel="nofollow">testing</a> package as a regular package in a regular go file... this is an example:</p>
<p><a href="https://golang.org/pkg/testing/#example_B_RunParallel" rel="nofollow">https://golang.org/pkg/testing/#example_B_RunParallel</a></p>
<p>EDIT: There is a function <code>func Benchmark(f func(b *B)) BenchmarkResult {}</code> that gives you a <code>BenchmarkResult</code> without outputting it.</p></pre>nasciiboy: <pre><p>brilliant! promising and interesting, I'll prove it tomorrow</p></pre>hell_0n_wheel: <pre><p>Why are you discounting <code>Testing.B</code>?</p>
<p>It's like you're asking "How to print to screen? (no fmt.Print)"</p></pre>nasciiboy: <pre><p>b.Log....? No, it is to make comparisons of regexps with several languages and libraries (comming son ^-^ (again...)), and the output of <code>test</code> is far from being appropriate for that work</p></pre>hell_0n_wheel: <pre><blockquote>
<p>the output of test is far from being appropriate for that work</p>
</blockquote>
<p>No idea what you're on about. The output of test is perfect for that use case. </p></pre>titpetric: <pre><p>How about using the same:</p>
<pre><code>//#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
}
</code></pre>
<p>Source: <a href="https://stackoverflow.com/a/31030753/112129" rel="nofollow">stackoverflow comment</a></p></pre>FUZxxl: <pre><p>Oh no please don't. cgo has a rather large overhead, distorting your measurements. </p></pre>titpetric: <pre><p>Want to give a stab at implementing it in assembly, or would it not work (ie, improve the situation)? :)</p></pre>FUZxxl: <pre><p>No. Just use the Go API. Though I guess you could tr to read the performance counters in assembly.</p></pre>nasciiboy: <pre><p><strong>EXCELSIOR!</strong></p>
<pre><code>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)
}
</code></pre>
<p>compiling</p>
<pre><code>$ go build main.go
</code></pre></pre>SpokenSpruce: <pre><p>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.</p>
<pre><code>startTime := time.Now()
// Do Stuff
result := time.Since(startTime)
</code></pre></pre>nasciiboy: <pre><p>time.Now(), returns system time, on the other hand, clock (C) returns an approximation of the processor time used by the program</p></pre>vietnq: <pre><p>You can use time API with Go 1.9 <a href="https://tip.golang.org/pkg/time/#hdr-Monotonic_Clocks" rel="nofollow">https://tip.golang.org/pkg/time/#hdr-Monotonic_Clocks</a></p></pre>nasciiboy: <pre><p>thanks for the info. I have updatedand and tested, althought the C clock() is more consistent. The official documetation... is a bit confusing </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传