14.16 基准测试

Kesa...小于 1 分钟

14.16 基准测试

package chapter_14

func channelSync(n int) {
	ch := make(chan int)
	go func() {
		for i := 0; i < n; i++ {
			ch <- i
		}
		close(ch)
	}()
	for range ch {
	}
}

func channelBuffered(n int) {
	ch := make(chan int, 128)
	go func() {
		for i := 0; i < n; i++ {
			ch <- i
		}
		close(ch)
	}()
	for range ch {
	}
}

package chapter_14

import "testing"

func BenchmarkChannelSync(b *testing.B) {
	channelSync(b.N)
}

func BenchmarkChannelBuffered(b *testing.B) {
	channelBuffered(b.N)
}

BenchmarkChannelSync
BenchmarkChannelSync-12          5635058               215.3 ns/op
BenchmarkChannelBuffered
BenchmarkChannelBuffered-12     19519368                61.28 ns/op
PASS
ok      the-way-to-go/chapter-14        2.731s
上次编辑于:
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.2