Skip to content

Conversation

sandeepkalra
Copy link

Go Impl.

Go/shortUrl.go Outdated
var s string
for n > 0 {
c := string(Alphabets[n%Base])
fmt.Println(c)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this println intended?

@sandeepkalra
Copy link
Author

sandeepkalra commented Jul 11, 2017 via email

@ocram
Copy link
Contributor

ocram commented Jul 11, 2017

Oh, totally forgot about this pull request. Sorry!

Are the Codec and InitShorteningCodec really necessary? What do they do?

Thanks for your work, @sandeepkalra!

@sandeepkalra
Copy link
Author

Yes, the InitShorteningCodec is required. It returns the instance/object of the structure that has Encode and Decode functions.

@sandeepkalra
Copy link
Author

I coded as per GoLang paradigm . The library offers a instance of itself called " Codec{} " by using "InitShorteningCodec()" that has access functions "Encode()" and "Decode()".

@ocram ocram mentioned this pull request Jul 14, 2017
@bgadrian
Copy link

Encode function is not optimal, it allocates a new string for each step, you should use a bytes.buffer or a Strings.builder

@sandeepkalra
Copy link
Author

sandeepkalra commented Jun 14, 2018

I wrote this before go ver1.10, and strings.builder wasn't there. I did try to play with it today after your comments, but the benchmark results didn't show anything promising for me to change.

Please see

===== code ====
// Note: This is not correct impl.,
// I should have to pre-append, but I am only measuring the
// time here, so let it run this way.

func (c *Codec) EncodeOpti(n int) string {
sb := strings.Builder{}
sb.Grow(n + 1)
for n > 0 {
sb.WriteByte(Alphabets[n%Base])
n /= Base
}
return sb.String()
}

func (c *Codec) Encode(n int) string {
var s string
for n > 0 {
s = string(Alphabets[n%Base]) + s
n /= Base
}
return s
}

=== benchmark ===
func BenchmarkEncode1(b *testing.B) {
en := InitShorteningCodec()
for i := 0; i < 1000000; i++ {
en.EncodeOpti(i)
}
}
func BenchmarkEncode2(b *testing.B) {
en := InitShorteningCodec()
for i := 0; i < 1000000; i++ {
en.Encode(i)
}
}
===== benchmark results ====

{Go}-> go test -bench=Encode2 (calling old code)
goos: darwin
goarch: amd64
BenchmarkEncode2-8 2000000000 0.08 ns/op
PASS
ok _/Users/kalra/projects/ShortURL/Go 1.672s
{Go}-> go test -bench=Encode1 (calling new code)
goos: darwin
goarch: amd64
BenchmarkEncode1-8 1 41155475176 ns/op
PASS
ok _/Users/kalra/projects/ShortURL/Go 41.168s

@sandeepkalra
Copy link
Author

I think my post earlier is a little too early. I have not studied strings.builder correctly I did another small change, and removed the sb.Grow(), and it seems to now run in 1/3rd time from previous. I will make changes to my code after I have full understanding of string.Builder library.

@ocram
Copy link
Contributor

ocram commented Jun 14, 2018

Thanks, @bgadrian, and thanks for testing this and developing a modified implementation, @sandeepkalra!

So this is now our third candidate for the Go implementation: #32

@ocram ocram mentioned this pull request Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants