-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Go and updates #15
base: davidaf3
Are you sure you want to change the base?
Conversation
I was looking at the solution you wrote in Java, and it seems really similar to what I had done... I couldn't figure out why your solution is faster... can you explain the difference? |
The main difference is that I group words by length when building a solution. For example, suppose that the phone number This is done by storing the partial solutions in an irregular matrix, where each row contains a list of words with the same length. To print the encodings I have to recursively build each posible solution using the words from the matrix, but to find the solution count I only need to multiply the lengths of the rows. That's why it saves so much time when only counting encodings. Also, my solution saves the most steps when a lot of words of the same length can be grouped. The english dictionary contains a lot of short words, so the probability of being able to group them is high. That's another reason why my solution saves a lot of time when counting. |
Ah! It didn't even come across my mind to optimise for the "count" case!! The only reason I introduced that case was to remove the IO overhead because the programs run so fast that after around 100,000 phone numbers, they spend nearly the whole time writing to stdout. I hadn't really comprehended that you had optimised for count specifically, but that makes a lot of sense now that you explain it. Even though that's really smart, I have to say that, for the purposes of benchmarking the languages' ability to run the actual algorithm to find all solutions, that would probably count as "cheating" (not saying you're cheating, just that we're not comparing solutions to the original problem anymore), no offense! Anyway, it's really cool to see just how fast the programs can get when you apply intelligent optimisations like that, and thank you very much for the effort. I ported the original Lisp solution to Zig! I have some work to do to get it working for long inputs, and there are lots of ways to optimise Zig programs, so I am hoping to do that before pushing a PR tomorrow with the results. I will then compare that against the Go/C/Java solutions you've given (the "print" variant at least)... I might even implement the Trie-based solution which apparently is always faster than the arithmetic one (not sure about that yet, that's actually one of the things I want to find out! The Rust solution removed arithmetic because it could do the same kind of thing by just storing bytes in the hashmap keys, which I am planning to do also in Zig). |
I actually wrote the solution before the count option was in the benchmark. When it was added I figured I could just multiply the lengths of the rows instead of counting each solution one by one, haha. I did a quick change to the Java code (
I guess the results really depend on the input generated. Sometimes it will benefit from grouping and sometimes not. I will be out the rest of the month so I won't be able to update the other languages until August. |
I have updated all the implementations to count the solutions one by one. I have also optimized the code that generates the solutions and now it runs way faster. These are the results that I got after running the benchmark again:
|
Oh nice, thanks for posting the new results! Looks interesting. |
Hi!
I have added a Go implementation and updated my old Java and C solutions. I ran (on WSL) the benchmark an these are the results I got: