5
\$\begingroup\$

I am working with some Java code. Basically I have a wrapper class that holds a string and implements some of the many useful python string methods in Java. My goal here is to implement the Python method .ljust and my hope is to be as efficient as possible. Currently, I am using a while loop which I imagine is terrible inefficient especially because it includes a +=. What's more, I am not sure which of the following alternatives is more efficient

String news = "";
news = this.toString();
int length = news.length();
while (length < size) {
 news += " ";
 length++;
}
return new PythonString(news);

In the above case, news.length() is only called once, but now there is an extra int and a new instruction length++.

The other option would be

String news = "";
news = this.toString();
while (news.length() < size) {
 news += " ";
}
return new PythonString(news);

In this case, news.length() is called every time.

I am interested in perspectives on which of these alternatives is most efficient as well as any other optimizations you could provide for this method, which is meant to pad a string with size spaces to the right so that all following characters line up at the same column.

asked Dec 24, 2015 at 8:25
\$\endgroup\$
1
  • 1
    \$\begingroup\$ you should actually measure the differences. There are several options to do this, as explained in the answers. \$\endgroup\$ Commented Dec 24, 2015 at 11:29

3 Answers 3

4
\$\begingroup\$

I am interested in perspectives on which of these alternatives is most efficient

Using StringBuilder in the loop instead of += will likely be far faster than either of your current approaches, especially as size - length gets big. I'm not sure how much more space it would take.

answered Dec 24, 2015 at 9:15
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Space-wise, likely normally less as you won't need to constantly reallocate. \$\endgroup\$ Commented Dec 25, 2015 at 10:27
5
\$\begingroup\$

The String class can do this already, see answer here: https://stackoverflow.com/a/391978/4217399. You can always look at the source of the String class to check how it's done, should be fairly optimized there, as it's a part of the JDK. It's actually offloaded to the java.util.Formatter class.

answered Dec 24, 2015 at 9:13
\$\endgroup\$
1
  • \$\begingroup\$ I thought I looked through the documentation, that is why i implemented these, but thanks for the info \$\endgroup\$ Commented Dec 24, 2015 at 16:13
4
\$\begingroup\$

What your code seems to be doing is to add spaces at the end of a string to make it at least size long.

You could write it in a way that better expresses your intent:

String news = this.toString(); //no need to allocate an empty string first
int padding = size - news.length();
return new PythonString(appendSpaces(news, padding));

And have a utility method that does the appending:

private static String appendSpaces(String s, int n) {
 if (n <= 0) return s;
 char[] spaces = new char[n];
 Arrays.fill(spaces, ' ');
 return s.concat(new String(spaces));
}

This will also be more efficient than your approach of concatenating the spaces one by one.

answered Dec 24, 2015 at 10:36
\$\endgroup\$
1
  • \$\begingroup\$ This. No need to reinvent the wheel when all the cogs are already there! \$\endgroup\$ Commented Dec 24, 2015 at 12:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.