[android-developers] Re: Simple Java Question

2009-08-25 Thread Shawn Brown

 HI,

 My garbage collection went from once every 6 seconds to once every 27
 seconds.

So you are happy with that!  Less need for garbage collection is a
good thing right!

 I did not try using a String. There was another post on the group that
 said a String would be more costly than a Formatter which is why I
 took the Formatter route.

Ah, yeah sorry.  I meant:

1 fomatter + 1 StringBuilder + (1*n) Strings [StringBuilder will make
a new string each toString() call]

is cheaper than

(1*n)Formatter + (1*n )String [again a new String will be generated by
the Formatter.toString() ]

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Simple Java Question

2009-08-24 Thread Shawn Brown

 Rather wasteful on a platform like Android. Are there any other
 options other than Formatter?

I think it does exactly what you want

          final Formatter f = new Formatter();

use Formatter(Appendable a)

 StringBuilder sb = new StringBuilder();

 final Formatter f = new Formatter(sb);

         for (Sentinel s : Sentinel.mSentinels) {
          if (s.mIsLive) {
                  pos++;
                f.format(%4d%5d, s.mPos.x, s.mPos.y);
             canvas.drawText(sb.toString(), x_pos, (pos *
mHalfHeight), mPaint);
              }
  sb.setLength(0);

see StringBuilder

toString

public String toString()

Returns a string representing the data in this sequence. A new
String object is allocated and initialized to contain the character
sequence currently represented by this object. This String is then
returned. Subsequent changes to this sequence do not affect the
contents of the String.

I assume a String is less costly than a Formatter.

Shawn

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Simple Java Question

2009-08-24 Thread Rud

I changed the code to use the StringBuilder and then made the
StringBuilder and Formatter static final private fields, instead of
local variables.

My garbage collection went from once every 6 seconds to once every 27
seconds.

That is what I thought might happen and why I considered the code I
posted as wasteful.

I did not try using a String. There was another post on the group that
said a String would be more costly than a Formatter which is why I
took the Formatter route.

Rud

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Simple Java Question

2009-08-23 Thread chinchin

Well you could flush the stream each time, wouldn't that be similar
with creating a new formatter? I used to do something similar when
reading from files etc.

On Aug 23, 1:18 am, Rud rudmerr...@gmail.com wrote:
 Rather wasteful on a platform like Android. Are there any other
 options other than Formatter?

 Rud

 On Aug 22, 10:28 am, Mark Murphy mmur...@commonsware.com wrote:

  Rud wrote:
   The is (maybe) a simple Java question but I can't find the anwer
   searching.

   The code is:

           final Formatter f = new Formatter();

           for (Sentinel s : Sentinel.mSentinels) {
               if (s.mIsLive) {
                   pos++;
                   f.format(%4d%5d, s.mPos.x, s.mPos.y);
                   canvas.drawText(f.toString(), x_pos, (pos *
   mHalfHeight), mPaint);
               }

   The problem is Formatter appends the output rather than clearing the
   underlying buffer for each 'format'. Okay, my bad. I looked for
   something to clear the underlying buffer but can't find anything.

   Suggestions?

  Create a new Formatter.

  If you look at the constructors for Formatter, you will see that its
  mission in life is to dump data to some stream-like object, be that a
  literal stream, or a File, or any sort of Appendable object. As such, it
   is aimed at calling format() multiple times to dump more data to the
  same output stream, then closing up the stream (via flush() and close()).

  If you want to dump data to some new stream (in your case, a new
  String), you need to create a new Formatter.

  --
  Mark Murphy (a Commons 
  Guy)http://commonsware.com|http://twitter.com/commonsguy

  Android App Developer Books:http://commonsware.com/books.html-Hide quoted 
  text -

  - Show quoted text -
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Simple Java Question

2009-08-22 Thread Mark Murphy

Rud wrote:
 The is (maybe) a simple Java question but I can't find the anwer
 searching.
 
 The code is:
 
 final Formatter f = new Formatter();
 
 for (Sentinel s : Sentinel.mSentinels) {
 if (s.mIsLive) {
 pos++;
 f.format(%4d%5d, s.mPos.x, s.mPos.y);
 canvas.drawText(f.toString(), x_pos, (pos *
 mHalfHeight), mPaint);
 }
 
 The problem is Formatter appends the output rather than clearing the
 underlying buffer for each 'format'. Okay, my bad. I looked for
 something to clear the underlying buffer but can't find anything.
 
 Suggestions?

Create a new Formatter.

If you look at the constructors for Formatter, you will see that its
mission in life is to dump data to some stream-like object, be that a
literal stream, or a File, or any sort of Appendable object. As such, it
 is aimed at calling format() multiple times to dump more data to the
same output stream, then closing up the stream (via flush() and close()).

If you want to dump data to some new stream (in your case, a new
String), you need to create a new Formatter.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books.html

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Simple Java Question

2009-08-22 Thread Rud

Rather wasteful on a platform like Android. Are there any other
options other than Formatter?

Rud


On Aug 22, 10:28 am, Mark Murphy mmur...@commonsware.com wrote:
 Rud wrote:
  The is (maybe) a simple Java question but I can't find the anwer
  searching.

  The code is:

          final Formatter f = new Formatter();

          for (Sentinel s : Sentinel.mSentinels) {
              if (s.mIsLive) {
                  pos++;
                  f.format(%4d%5d, s.mPos.x, s.mPos.y);
                  canvas.drawText(f.toString(), x_pos, (pos *
  mHalfHeight), mPaint);
              }

  The problem is Formatter appends the output rather than clearing the
  underlying buffer for each 'format'. Okay, my bad. I looked for
  something to clear the underlying buffer but can't find anything.

  Suggestions?

 Create a new Formatter.

 If you look at the constructors for Formatter, you will see that its
 mission in life is to dump data to some stream-like object, be that a
 literal stream, or a File, or any sort of Appendable object. As such, it
  is aimed at calling format() multiple times to dump more data to the
 same output stream, then closing up the stream (via flush() and close()).

 If you want to dump data to some new stream (in your case, a new
 String), you need to create a new Formatter.

 --
 Mark Murphy (a Commons 
 Guy)http://commonsware.com|http://twitter.com/commonsguy

 Android App Developer Books:http://commonsware.com/books.html- Hide quoted 
 text -

 - Show quoted text -
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---