0

I am writing a file with possibly 1000 data points. I have classes for all of these and am currently writing all of the data at the end (the datapoints are taken at 1s intervals). What I am currently doing is written below, and it's very slow. Would I be better off changing how I am writing the string/bytes to the file? Or would I be better off writing this information to some file pointer as the application is running?

Btw, all of the things such as getAccuracy() and such are floats/ints (so it has to convert those also).

fileStr = "";
        fileStr += "timestamp,Accuracy,Altitude,Latitude,Longitude,GPSSatelliteEntries\r\n";
        for (Iterator<Entry> i = entries.iterator(); i.hasNext(); ) {
            Entry item = i.next();
            long ts = item.getTs();
            DataEntry d = item.getD();
            List<GPSSatelliteEntry> satellites = item.getG();

            // write stuff
            fileStr += ts + ",";
            fileStr += d.getAccuracy() + "," + d.getAltitude() + "," + d.getLatittude() + "," + d.getLongitude() + ",";
            fileStr += "[";
            boolean entered = false;
            for (Iterator<GPSSatelliteEntry> j = satellites.iterator(); j.hasNext(); ) {
                GPSSatelliteEntry item2 = j.next();
                entered = true;
                fileStr += "(" + item2.getAzimuth() + "," + item2.getElevation() + "," + item2.getPrn() + "," + item2.getSnr() + "),";
            }
            // chop off extra ,
            if (entered)
                fileStr = fileStr.substring(0, fileStr.length() - 1);
            fileStr += "]";
            fileStr += "\r\n";
        }
MasterGberry
  • 2,800
  • 6
  • 37
  • 56

3 Answers3

4

Everytime you have hard work with Strings, use StringBuilder or StringBuffer to achieve better performance .

Don't forget that String is immutable, and each time you modify String new instance will be created and it costs performance.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • What exactly is "hard work"? If String is immutable, then how can it be modified? And how does creating a new `String" "costs performance"? – Steve Kuo Apr 16 '13 at 17:06
  • Any work is hard, I suggest doing as little of it as possible. Creating a new string "costs performance" because there are operations related to memory allocation, pointers, and other things that happen when you create one. Using something like a StringBuilder will allow you to save this overhead because you are using a single object. When applied over a large data set you can see significant differences in performance. – RacerNerd Apr 19 '13 at 21:39
0

Most probably string buffer

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.

or go for string builder

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0
StringBuilder stuff = new StringBuilder();
stuff.append("PUT YOUR STRINGS HERE");
stuff.append("PUT YOUR STRINGS HERE");

Then you can use 'stuff' to print the strings. Put it in a loop and iterate over a large number with a timer to see the advantages, it's pretty interesting.

RacerNerd
  • 1,579
  • 1
  • 13
  • 31
  • 1
    Or chaining ofcourse, which in this case might just be useful. – Menno Apr 12 '13 at 20:23
  • Chaining is an excellent suggestion here. Chaining rather than using string concatenation within each call to append will save cycles. – RacerNerd Apr 19 '13 at 21:33