4. Which one is faster?
type MyTest struct {
param1 uint64
param2 uint64
}
var addTimes = 100000000
var wg sync.WaitGroup
func Inc(num *uint64) {
for i := 0; i < addTimes; i++ {
atomic.AddUint64(num, 1)
}
wg.Done()
}
func BenchmarkTestProcessNum1(b *testing.B) {
runtime.GOMAXPROCS(1)
myTest := &MyTest{}
wg.Add(2)
go Inc(&myTest.param1)
go Inc(&myTest.param2)
wg.Wait()
}
type MyTest struct {
param1 uint64
param2 uint64
}
var addTimes = 100000000
var wg sync.WaitGroup
func Inc(num *uint64) {
for i := 0; i < addTimes; i++ {
atomic.AddUint64(num, 1)
}
wg.Done()
}
func BenchmarkTestProcessNum2(b *testing.B) {
runtime.GOMAXPROCS(2)
myTest := &MyTest{}
wg.Add(2)
go Inc(&myTest.param1)
go Inc(&myTest.param2)
wg.Wait()
}
6. Which one is faster?
type MyTest struct {
param1 uint64
param2 uint64
}
var addTimes = 100000000
var wg sync.WaitGroup
func Inc(num *uint64) {
for i := 0; i < addTimes; i++ {
atomic.AddUint64(num, 1)
}
wg.Done()
}
func BenchmarkTestProcessNum1(b *testing.B) {
runtime.GOMAXPROCS(1)
myTest := &MyTest{}
wg.Add(2)
go Inc(&myTest.param1)
go Inc(&myTest.param2)
wg.Wait()
}
type MyTest struct {
param1 uint64
param2 uint64
}
var addTimes = 100000000
var wg sync.WaitGroup
func Inc(num *uint64) {
for i := 0; i < addTimes; i++ {
atomic.AddUint64(num, 1)
}
wg.Done()
}
func BenchmarkTestProcessNum2(b *testing.B) {
runtime.GOMAXPROCS(2)
myTest := &MyTest{}
wg.Add(2)
go Inc(&myTest.param1)
go Inc(&myTest.param2)
wg.Wait()
}