Skip to content
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

Open
RobertZenz opened this issue Feb 21, 2017 · 13 comments
Open

Implementations seem inefficient. #20

RobertZenz opened this issue Feb 21, 2017 · 13 comments

Comments

@RobertZenz
Copy link
Collaborator

Most implementations seem a little bit inefficient, for example the Java version could be optimized to this:

class yes {
    public static void main (String[] args) {
        if (args.length == 0) {
            while (true) {
                System.out.println("y");
            }
        } else {
            String line = "";
            for (String arg : args) {
                line = line + arg + " ";
            }
            line = line.substring(0, line.length() - 1);
            
            while (true) {
                System.out.println(line);
            }
        }
    }
}

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 a StringBuilder 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.

@TheBinaryFox
Copy link
Collaborator

TheBinaryFox commented Feb 22, 2017

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);
            }
        }
    }
}

@mubaris
Copy link
Owner

mubaris commented Feb 22, 2017

Thanks for the suggestion @RobertZenz . I will work on that 👍

@RobertZenz
Copy link
Collaborator Author

@TheBinaryFox Thanks, from time to time I forget about the new methods.

@mubaris While looking at it again, the class should be called Yes according to the Java naming conventions. Also, in the "standard" there is no space between function names and the parentheses. But that's overruled by personal preference.

And looking at it again (thinking isn't exactly my strong skill past midnight), you could do something like this:

String output = "y";

if (args.length > 0) {
    output = String.join(" ", args); // Or any other string concatenation.
}

while (true) {
    System.out.println(output);
}

@mubaris
Copy link
Owner

mubaris commented Feb 22, 2017

@RobertZenz I like the latest version. This is concise.

This was referenced Feb 22, 2017
@ladydascalie
Copy link
Contributor

@RobertZenz I've PR'd two potential improvements for the PHP and Go version, you are more than welcome to have a look at

@mubaris
Copy link
Owner

mubaris commented Feb 22, 2017

@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 yes instead of Yes, because that's the command name.

@RobertZenz
Copy link
Collaborator Author

@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).

@ladydascalie
Copy link
Contributor

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

@RobertZenz
Copy link
Collaborator Author

@ladydascalie Why use a switch if there are only two cases?

@ladydascalie
Copy link
Contributor

@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 👍

@maferland
Copy link

I thought I could improve the python version. I removed a useless ; and the else statement achieved nothing. #88

@Scan0r
Copy link

Scan0r commented Dec 16, 2019

@TheBinaryFox Thanks, from time to time I forget about the new methods.

@mubaris While looking at it again, the class should be called Yes according to the Java naming conventions. Also, in the "standard" there is no space between function names and the parentheses. But that's overruled by personal preference.

And looking at it again (thinking isn't exactly my strong skill past midnight), you could do something like this:

String output = "y";

if (args.length > 0) {
    output = String.join(" ", args); // Or any other string concatenation.
}

while (true) {
    System.out.println(output);
}

Improving it even more you could write:

String output = args.length > 0 ? String.join(" ", args) : "yes";
while(true)
System.out.println(output);

@RobertZenz
Copy link
Collaborator Author

@Scan0r That's not improved, that's just shorter (and not in the more readable way I might argue).

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

No branches or pull requests

6 participants