Best system for drawing ints to screen?

I have a lot of data stored as ints that needs to be drawn once per second or so. (As in “you have 8 widgets available”, where the 8 can reasonably be expected to change every couple of seconds - and then 20 kinds of widgets) Graphics objects can draw byte arrays, char arrays, and Strings. What is the most efficient way to convert? I guess I should say that I don’t expect the numbers to ever be larger than 32,000 (although they will be larger than 255).

Thanks for any help!

Totally implementation dependant, sorry. The answer can change depending on the VM.

An intelligent VM maker will likely try ot optimize the expected most-used path. My guess would be that woudl be strings.

BUT I have to wonder if you aren’t worrying about the wrong things. Have you profiled your app yet? Is this really a bottleneck??

I just put it into Strings… then I call my drawText method with it. It uses toCharArray() in order to create a char array were I can loop through… then I just use that char number to determine wich Image I should draw and that’s it.

I can’t imagine how that could be a bottleneck. This way I can draw more than 20000 Strings per second on a half decent PC.

There isn’t much room for optimisations. A method wich is twice as fast would mean I would get 0.00001 fps more (dramatisation :))… but even if it’s 0.1fps - who would notice that?

Ah I think I understand. I think I phrased my question poorly - I’m not worried about the drawing, I’m worried about the allocation of hundreds of strings per second (is that something to worry about?). But if I understand oNyx correctly, I should just have a static final array of Chars, and draw those.

Please reply if I missed something. Thanks for the help!

I should just have a static final array of Chars, and draw those.

Hm… well I draw (managed) Images…

I load it, place each char into a seperate Image (52 or so) and just draw the letters I need.


public void drawText(Graphics2D g, String text, Color col, int scale, int xOff, int yOff)
{
      char[] t=text.toCharArray();

      g.setColor(col);
      g.fillRect(xOff,yOff,6*scale*t.length,5*scale);

      int c=43;
      for(int i=0;i<t.length;i++)
      {
            switch(t[i])
            {
                  case 'a':  c= 0;break;
                  case 'b':  c= 1;break;
                  case 'c':  c= 2;break;
                  case 'd':  c= 3;break;
                  case 'e':  c= 4;break;
                  case 'f':  c= 5;break;
                  case 'g':  c= 6;break;
                  [...etc...]
            }
            if(c!=-1)
                  g.drawImage(chars[c],xOff+i*6*scale,yOff,6*scale,5*scale,null);
      }

You get the idea? Obviously it’s easier if the chars match their natural order (e.g. no switch at all and just g.drawImage(chars[c&0xff]…)).

I’m worried about the allocation of hundreds of strings per second

Well that wasn’t a problem for me or even noticable (on a 4+ year old PC). However it’s “relativly” slow if you use drawString… it also produces a fair amout of garbage. Switch to images and it’s about as fast as it can get… oh and it’s cuter :slight_smile:

If you are still worried about Strings… well just write a simiar method wich takes an int as argument, tear it apart to get the digits and… well you see? It’s about as expensive as using Strings - after all an Array is also just an object :wink:

Well just profile your code and find the real bottlenecks and spend time on speeding them up.

Images…yes that does make the most sense in terms of cost/benefit. Thank you very much for your help. As a followup, is there a good profiler that plugs into NetBeans? Or do I have to write my own? Thanks,

Bret

I dotn know if there is an integrated profiler.

There are some wonderful commecial profiling tools that you can use with netbeans on the same VM. (eg OptimizeIt, True Time, etc)

There is also a basic post-mortem profiler built into the VM itself (see -hprof).

Another tool to help with optimisation is Jfluid:
http://research.sun.com/projects/jfluid/

The most recent version has stuff to track object liveliness and can be used to find memory leaks. I used it for just such a thing last week!

If you’d like to see this become a standard feature of Java 1.5 then vote for this RFE:
http://developer.java.sun.com/developer/bugParade/bugs/4879835.html