Skip to content

Commit

Permalink
Improove performace using a string builders instead of a string (#102)
Browse files Browse the repository at this point in the history
Thank you very much for the improvement and congrats for your first contribution.

LGTM, merging.
  • Loading branch information
4k1k0 authored Jul 16, 2022
1 parent eeb258a commit e683f8d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
10 changes: 6 additions & 4 deletions binarystring.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package faker

//BinaryString is the faker struct for BinaryString
import "strings"

// BinaryString is the faker struct for BinaryString
type BinaryString struct {
faker *Faker
}

// BinaryString returns a random binarystring of given input length
func (bn BinaryString) BinaryString(length int) string {
var bs string
var bs strings.Builder
for i := 0; i < length; i++ {
bs += bn.faker.RandomStringElement([]string{"0", "1"})
bs.WriteString(bn.faker.RandomStringElement([]string{"0", "1"}))
}
return bs
return bs.String()
}
10 changes: 9 additions & 1 deletion binarystring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

//tests BinaryString()
// tests BinaryString()
func TestBinaryString(t *testing.T) {
f := New().BinaryString()
inputLength := 11
Expand All @@ -21,3 +21,11 @@ func TestBinaryString(t *testing.T) {
}
Expect(t, true, isStringValid)
}

func BenchmarkBinaryString(b *testing.B) {
f := New().BinaryString()
inputLength := 100
for i := 0; i < b.N; i++ {
_ = f.BinaryString(inputLength)
}
}
18 changes: 10 additions & 8 deletions company.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,29 @@ type Company struct {
}

// CatchPhrase returns a fake catch phrase for Company
func (c Company) CatchPhrase() (phrase string) {
func (c Company) CatchPhrase() string {
var phrase strings.Builder
for i, words := range catchPhraseWords {
if i > 0 {
phrase += " "
phrase.WriteString(" ")
}
phrase += c.Faker.RandomStringElement(words)
phrase.WriteString(c.Faker.RandomStringElement(words))
}

return
return phrase.String()
}

// BS returns a fake bs words for Company
func (c Company) BS() (bs string) {
func (c Company) BS() string {
var bs strings.Builder
for i, words := range bsWords {
if i > 0 {
bs += " "
bs.WriteString(" ")
}
bs += c.Faker.RandomStringElement(words)
bs.WriteString(c.Faker.RandomStringElement(words))
}

return
return bs.String()
}

// Suffix returns a fake suffix for Company
Expand Down
14 changes: 14 additions & 0 deletions company_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,27 @@ func TestCompanyCatchPhrase(t *testing.T) {
Expect(t, 2, strings.Count(phrase, " ")) // 3 words
}

func BenchmarkCompanyCatchPhrase(b *testing.B) {
f := New().Company()
for i := 0; i < b.N; i++ {
_ = f.CatchPhrase()
}
}

func TestCompanyBS(t *testing.T) {
f := New().Company()
bs := f.BS()
Expect(t, true, len(bs) > 0)
Expect(t, 2, strings.Count(bs, " ")) // 3 words
}

func BenchmarkCompanyBS(b *testing.B) {
f := New().Company()
for i := 0; i < b.N; i++ {
_ = f.BS()
}
}

func TestCompanySuffix(t *testing.T) {
f := New().Company()
value := f.Suffix()
Expand Down

0 comments on commit e683f8d

Please sign in to comment.