Skip to content

Conversation

mohamadnabikhani
Copy link

It's seem that go version code is slower than python and js and it didn't make sense for me, so I looked at the code and for some reason if statement inside a loop make the code slower so I rewrite it and move the if statement to for conventional statement. here is the benchmark result I've got for before and after the change:

before:
BenchmarkPrime-8 311378482 3.755 ns/op 0 B/op 0 allocs/op

after:
BenchmarkPrime-8 1000000000 0.3114 ns/op 0 B/op 0 allocs/op

it's like the updated code is 10x faster than the old one.

@mohamadnabikhani mohamadnabikhani deleted the fix/optimize-go branch April 29, 2023 17:34
@ali77gh
Copy link
Owner

ali77gh commented Apr 29, 2023

The answer is wrong because execution will exit the loop when first non-prime number found.

❯ go run prime.go
0
0ms%  

But "Pashmam Rikht" for a second.

@mohamadnabikhani
Copy link
Author

Me too. :))))
Sorry, I know. I forget running this script. that's why I closed this pull request. still working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants