Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created July 10, 2021 19:36
Show Gist options
  • Save ttsugriy/2f52475c4064b117cbbce5db3b75d7c9 to your computer and use it in GitHub Desktop.
Save ttsugriy/2f52475c4064b117cbbce5db3b75d7c9 to your computer and use it in GitHub Desktop.
Benchmarking go:embed directive
package main
import (
"embed"
"io/ioutil"
"testing"
)
//go:embed citadel_of_the_star_lords.txt
var f embed.FS
//go:embed citadel_of_the_star_lords.txt
var content string
//go:embed citadel_of_the_star_lords.txt
var bytes []byte
func mostCommonWordEmbed() int {
data, _ := f.ReadFile("citadel_of_the_star_lords.txt")
return findMostFrequentWord(string(data))
}
func mostCommonWordFs() int {
data, _ := ioutil.ReadFile("citadel_of_the_star_lords.txt")
return findMostFrequentWord(string(data))
}
func mostCommonWordString() int {
return findMostFrequentWord(content)
}
func mostCommonWordBytes() int {
return findMostFrequentWord(string(bytes))
}
func findMostFrequentWord(text string) int {
return len(text)
}
func BenchmarkCommonWordEmbedded(b *testing.B) {
total := 0
for i := 0; i < b.N; i++ {
total += mostCommonWordEmbed()
}
}
func BenchmarkCommonWordFs(b *testing.B) {
total := 0
for i := 0; i < b.N; i++ {
total += mostCommonWordFs()
}
}
func BenchmarkCommonWordString(b *testing.B) {
total := 0
for i := 0; i < b.N; i++ {
total += mostCommonWordString()
}
}
func BenchmarkCommonWordBytes(b *testing.B) {
total := 0
for i := 0; i < b.N; i++ {
total += mostCommonWordBytes()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment