Skip to content

Instantly share code, notes, and snippets.

@soypat
Created February 12, 2021 18:02
Show Gist options
  • Save soypat/83f9358661c44656c7edbff03863a558 to your computer and use it in GitHub Desktop.
Save soypat/83f9358661c44656c7edbff03863a558 to your computer and use it in GitHub Desktop.
Cgo usleep vs. pure Go time.Sleep. Run with `go test -bench=.`
package ctest
// #include <time.h>
// void wait(int usec)
// {
// usleep(usec);
// }
import "C"
import "time"
func CUsleep(i int) {
C.wait(C.int(i))
}
func GUsleep(i int) {
time.Sleep(time.Duration(i) * 1000)
}
package ctest_test
import (
"bench/ctest"
"testing"
)
func benchGUSleep(u int, b *testing.B) {
for i := 0; i < b.N; i++ {
ctest.GUsleep(u)
}
}
func benchCUSleep(u int, b *testing.B) {
for i := 0; i < b.N; i++ {
ctest.CUsleep(u)
}
}
func BenchmarkCUsleep0(b *testing.B) { benchCUSleep(0, b) }
func BenchmarkCUsleep1(b *testing.B) { benchCUSleep(1, b) }
func BenchmarkCUsleep2(b *testing.B) { benchCUSleep(2, b) }
func BenchmarkCUsleep10(b *testing.B) { benchCUSleep(10, b) }
func BenchmarkCUsleep50(b *testing.B) { benchCUSleep(50, b) }
func BenchmarkCUsleep100(b *testing.B) { benchCUSleep(100, b) }
func BenchmarkCUsleep1000(b *testing.B) { benchCUSleep(1000, b) }
func BenchmarkGUsleep0(b *testing.B) { benchGUSleep(0, b) }
func BenchmarkGUsleep1(b *testing.B) { benchGUSleep(1, b) }
func BenchmarkGUsleep2(b *testing.B) { benchGUSleep(2, b) }
func BenchmarkGUsleep10(b *testing.B) { benchGUSleep(10, b) }
func BenchmarkGUsleep50(b *testing.B) { benchGUSleep(50, b) }
func BenchmarkGUsleep100(b *testing.B) { benchGUSleep(100, b) }
func BenchmarkGUsleep1000(b *testing.B) { benchGUSleep(1000, b) }
@ljx0517
Copy link

ljx0517 commented Nov 5, 2021

what is the result?

@soypat
Copy link
Author

soypat commented Nov 5, 2021

@ljx0517

$ go version
go version go1.15.2 linux/amd64
BenchmarkCUsleep0-4      	   20474	     59241 ns/op	       0 B/op	       0 allocs/op
BenchmarkCUsleep1-4      	   20268	     59830 ns/op	       0 B/op	       0 allocs/op
BenchmarkCUsleep2-4      	   19390	     61233 ns/op	       0 B/op	       0 allocs/op
BenchmarkCUsleep10-4     	   17448	     68566 ns/op	       0 B/op	       0 allocs/op
BenchmarkCUsleep50-4     	   10000	    111465 ns/op	       0 B/op	       0 allocs/op
BenchmarkCUsleep100-4    	    6894	    166218 ns/op	       0 B/op	       0 allocs/op
BenchmarkCUsleep1000-4   	    1053	   1157965 ns/op	       0 B/op	       0 allocs/op
BenchmarkGUsleep0-4      	421654693	         2.83 ns/op	       0 B/op	       0 allocs/op
BenchmarkGUsleep1-4      	  204738	      5826 ns/op	       0 B/op	       0 allocs/op
BenchmarkGUsleep2-4      	  118904	      8905 ns/op	       0 B/op	       0 allocs/op
BenchmarkGUsleep10-4     	   15459	     77754 ns/op	       0 B/op	       0 allocs/op
BenchmarkGUsleep50-4     	   14258	     84088 ns/op	       0 B/op	       0 allocs/op
BenchmarkGUsleep100-4    	    6842	    174307 ns/op	       0 B/op	       0 allocs/op
BenchmarkGUsleep1000-4   	    1028	   1168554 ns/op	       0 B/op	       0 allocs/op

@ljx0517
Copy link

ljx0517 commented Nov 5, 2021

oh dude~.thanks~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment