1

I have a loop like:

String tmp;

for(int x = 0; x < 1000000; x++) {


   // use temp


   temp = ""; // reset
}

This string is holding at most 10 characters.

What would be the most effecient way of creating a variable for this use case?

Should I use a fixed size array? Or a stringbuffer instead?

I don't want to create 1million variables when I don't have to, and it matters for this method (performance).

Edit I simplified my scenerio, I actually need this variable to be at the class level scope as there are some events that take place i.e. it can't be declared within the loop.

codecompleting
  • 9,251
  • 13
  • 61
  • 102

5 Answers5

2

Why not simply declare temp inside the loop like so:

for(int x = 0; x < 1000000; x++) {
    String temp;
    // use temp
}

You even get a very (very, very) slight performance increase because you don't have to waste time resetting the value of temp to "".

With regards to your update, It still depends on what you do with temp but a StringBuffer would probably be the easiest to use. And especially if you need to concatenate together a Sting, it would be quite fast.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
  • Well, i suppose you cannot just declare the variable inside the loop if you have something to do with its value from before. – nebula Dec 06 '11 at 14:39
  • @aneal It looks `temp` is declared immediately before the loop starts and reset to `""` at the last iteration so I doubt its value is set before the loop or used after. – Jack Edmonds Dec 06 '11 at 14:42
  • it has to be at object level scope, sorry for not saying that. – codecompleting Dec 06 '11 at 14:47
  • for each loop, I'm simply setting the value, then I compare against it along the way. I could even use an enumeration now that I think about it. – codecompleting Dec 06 '11 at 14:58
  • 1
    @codecompleting If you can use an enumeration, I would go for it. Comparing two values from an enumeration is much faster than comparing two strings. (You would have to compare every character in both strings before determining that the two strings are equal versus comparing the values of two enum constants.) – Jack Edmonds Dec 06 '11 at 15:01
0

Whats the problem with using fixed array? I think array will do. Here is similar question i found Making a very large Java array

Well, stringbuffer or StringBuilder will do too. But stringBuilder is fast than stringBuffer.

And if it based on the performance level, i think you might want to check the types of loops that give better performance.

Community
  • 1
  • 1
nebula
  • 3,932
  • 13
  • 53
  • 82
0

What exactly are you looking to do with tmp (or temp)?

Honestly, I'd just try declaring your variables within the loop if they aren't needed afterwards, and profile it. Many of the obscurities that have been used in the past to help with performance issues within loops are no longer needed in recent versions of Java, due to optimizations and other improvements in the compiler and the Hotspot JVM.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
0

The answer really depends on what you do with temp in the loop.

String instances are immutable by definition. If your processing includes string manipulation, you should not use String since you'll end up creating a lot of unnecessary very short-lived immutable instances. In this case use StringBuilder (or StringBuffer if thread-safety is required) instead.

If you merely create a new String (or obtain it from an external source) in every iteration and use it without any string manipulation operations that create new String objects, then you're OK using String. Note that creating a new String instance every iteration is usually quite fast and unless your profiler specifically points to this being a problem, you should not attempt to optimize this prematurely.

Note, also, that unless you specifically rely in each iteration on temp initial value being a reference to an empty string, there is no need to do temp = ""

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
0

Try this

public class Robal {

public void looping()
{
    for(int x = 0; x < 1000000; x++) {
        String temp=x+"";
        System.out.println(temp);
           temp = ""; // reset
        }

}
Noufal Panolan
  • 1,357
  • 2
  • 14
  • 27