本帖最后由 赚小钱 于 2020-6-15 20:11 编辑
哎呀,看错了。原来是到毫秒级别。那 golang 妥妥的支持啊。我的垃圾 cpu 能到 18 微秒。
以下是原回复,懒得修改了。
结果比较遗憾。时间精度达不到 μs 的级别。锁,系统调用的时间开销比较大。我去试一下 rust。package main
import (
"fmt"
"time"
)
func main() {
var (
totalTime time.Duration
count int
)
c := make(chan time.Duration, 10)
go func() {
for i := 0; i < 10; i++ {
start := time.Now()
time.Sleep(time.Microsecond)
c <- time.Since(start)
}
close(c)
}()
for value := range c {
totalTime += value
count++
fmt.Printf("elapsed %s\n", value)
}
fmt.Printf("avg: %s\n", totalTime/time.Duration(count))
}
elapsed 31.615µs
elapsed 77.752µs
elapsed 14.408µs
elapsed 17.315µs
elapsed 6.637µs
elapsed 7.053µs
elapsed 5.847µs
elapsed 6.597µs
elapsed 6.563µs
elapsed 6.346µs
avg: 18.013µs
cpu 也不太行
processor : 3
vendor_id : AuthenticAMD
cpu family : 21
model : 96
model name : AMD Opteron(tm) X3421 APU
stepping : 1
microcode : 0x6006118
cpu MHz : 1563.177
cache size : 1024 KB
physical id : 0
siblings : 4
core id : 3
cpu cores : 2
apicid : 19
initial apicid : 3
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good acc_power nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core perfctr_nb bpext ptsc mwaitx cpb hw_pstate ssbd vmmcall fsgsbase bmi1 avx2 smep bmi2 xsaveopt arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif overflow_recov
bugs : fxsave_leak sysret_ss_attrs null_seg spectre_v1 spectre_v2 spec_store_bypass
bogomips : 4192.27
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro acc_power [13]
|