Question

I am reading a data in byte-by-byte. when i have determined that i have an entire message, i need to pass it to another function as a string. Some messages can be quite large, but the sizes vary frequently. Which implementation do you all feel is most effecient:

public test class
{
   char[] buffer = new char[MAX_SIZE_7200];
   int bufferIndex = 0;
   void parseData(ArrayList<Byte> msg, length)
   {
       while (!msg.isEmpty())
       {
          buffer[bufferIndex++] = (char) msg.remove(0);
          if (isfullmessage)
          {
             parseData(new String(buffer, 0, bufferIndex);
             bufferIndex = 0;  //restart and continue parsing data
          }
       }
   }
 }

OR:

public test class
{
   List<Character> buffer = new ArrayList<Character>();
   int bufferIndex = 0;
   void parseData(ArrayList<Byte> msg, length)
   {
       while (!msg.isEmpty())
       {
          buffer.add((char) msg.remove(0));
          if (isfullmessage)
          {
              StringBuilder builder = new StringBuilder(buffer.size());
              for (Character ch: buffer)
              {
                  builder.append(ch);
              }
              parseData(builder.toString());
              buffer.clear();
          }
       }
   }
 }  

OR:

public test class
    {
       StringBuilder buffer = new StringBuilder();
       int bufferIndex = 0;
       void parseData(ArrayList<Byte> msg, length)
       {
           while (!msg.isEmpty())
           {
              buffer.append((char) msg.remove(0));
              if (isfullmessage)
              {                  
                  parseData(builder.toString());
                  buffer.clear();  //some stringbuilder clear function
              }
           }
       }
     }  

or is there a more efficient way. Please note that i have the variables holding my finished message outside the scope of the function since i might process data that does not contain a full message and may take multiple executions of the function to get a full message and process it.

Was it helpful?

Solution

Use StringBuilder. It supports appending characters one at a time, expands capacity as needed, and can be reset for reuse purposes.

OTHER TIPS

I think this depends on the average length of your messages. For a more 'full' message, the char will be a better choice since its element type

For the sparse case, the list template will be better as the memory consumption will be lower, despite the overhead of the object presentation

You are iterating over the elements of the ArrayList msg by removing the first element of this ArrayList in a loop. An ArrayList stores all its elements in an array. Removing the first element is slow, because all elements in the array (except the first one) need to be copied.

So maybe the biggest overhead is not in setting a character in an array or appending a character to a StringBuilder, but maybe the biggest overhead is in the repeated calls to msg.remove(0).

You can fix this by using this:

int index = 0;
while (index < msg.length()) {
    buffer[bufferIndex++] = (char) msg.get(index);
    index++;
    // etc.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top