-
Notifications
You must be signed in to change notification settings - Fork 43
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
Implementations seem inefficient. #20
Comments
The optimized Java version could be further simplified: class yes {
public static void main (String[] args) {
if (args.length == 0) {
while (true) {
System.out.println("y");
}
} else {
String line = args[0];
for (int i = 1; i < args.length; i++) {
line = line + " " + args[i];
}
while (true) {
System.out.println(line);
}
}
}
} Or for Java 8: class yes {
public static void main (String[] args) {
if (args.length == 0) {
while (true) {
System.out.println("y");
}
} else {
String line = String.join(" ", args);
while (true) {
System.out.println(line);
}
}
}
} |
Thanks for the suggestion @RobertZenz . I will work on that 👍 |
@TheBinaryFox Thanks, from time to time I forget about the new methods. @mubaris While looking at it again, the class should be called And looking at it again (thinking isn't exactly my strong skill past midnight), you could do something like this:
|
@RobertZenz I like the latest version. This is concise. |
@RobertZenz I've PR'd two potential improvements for the PHP and Go version, you are more than welcome to have a look at |
@RobertZenz I have implemented the efficient version in Java 8. Thanks to you and @TheBinaryFox . And about naming conventions, I thought about standard naming convention when I wrote the code for the first time. And I decided to use |
@ladydascalie Never read Go before, but looks good. Except the indentation of the PHP version is fucked up (there are also two spaces after the equals sign). |
PR #24 adds an even better go implementation eliminating duplicate for loops and using a switch to be more concise / readable, I'll correct that PHP indentation issue as well |
@ladydascalie Why use a |
@RobertZenz agreed, if you check my pr for the go version, I removed that, I hadn't done it on the PHP one though. I've just done that now however 👍 |
I thought I could improve the python version. I removed a useless |
Improving it even more you could write: String output = args.length > 0 ? String.join(" ", args) : "yes"; |
@Scan0r That's not improved, that's just shorter (and not in the more readable way I might argue). |
Most implementations seem a little bit inefficient, for example the Java version could be optimized to this:
We first build the string which will be one line, and then simply dump that string into the output stream. This removes the need for the system to constantly parse
"%s "
, it also removes that trailing space from each line which shouldn't be there.As a sidenote, yes, I could have used a
StringBuilder
, but opted against it for simplicity reasons and simply because the string, hopefully, never becomes that large to warrant aStringBuilder
there.The same applies to the C, Go, PHP, Python versions, and to a lesser extend to nearly all other versions, which are constantly looping over the arguments instead of caching the line once.
The text was updated successfully, but these errors were encountered: