0

I wrote a program to solve the following:

Implement a diffusion limited aggregation simulation on a toroid plane where seeds are randomly created, and particles move randomly. they move if they particles do not land near a seed or a particle. where user inputs seeds (red pixels), particles (black pixels), steps (no or iterations), plane size.

My code is very slow. How can I make it faster?

I randomly created x and y coordinates and drew red pixels (seeds), then randomly created x and y for black pixels (particles), if a black pixel lands where there is a red or black pixel it can stay, otherwise it moves randomly again until there are no more particles . If the pixel lands out of borders like x > border then x=0; if x <1 then x= border. The same for y.

This just means that if it lands on the border I move it to the opposite border. Then checks for the neighboring pixels again. I have an outer loop to create the seeds, and inner loop for the particles. In the inner loop I check for the x,y positions:

//Place, move, and "stick" the particles; stop if either steps or particles = 0
for (int p = 0; p < particles; p++) {

    for (int s = 0; s < steps; s++) {
        if (xPos > image.getWidth() ) {
                       do something
                    } 
        else if (xPos < 1) {
                        do something
                    }
        if (yPos > image.getHeight() - 2) {
                         do something
                    } 
        else if (yPos < 1) {
                         do something
                    } 
        else if (xPos > image.getWidth() && yPos > image.getHeight()) {
                         do something
                    } 
        else if (xPos < 1 && yPos < 1) {
                         do something
                    }

     //If the surrounding pixels' color is not white, make that particle stick.
            if (moveValid()) {
                image.setRGB(xPos, yPos, 0xFF000000);
            } 


            //Otherwise, move in a random direction
            else {
                if(xPos == 1 && image.getRGB(size - 2, yPos) != 0xFFFFFFFF){
                   draw(xPos,yPos);
                }
                else if(xPos == size - 2 && image.getRGB(1,yPos) != 0xFFFFFFFF){
                    draw(xPos,yPos);
                }
                if(yPos == 1 && image.getRGB(xPos, size - 2) != 0xFFFFFFFF){
                   draw(xPos,yPos);
                }
                else if(yPos == size - 2 && image.getRGB(xPos,1) != 0xFFFFFFFF){
                    draw(xPos,yPos);
                }
                else {
                    move();

                }


            }
        }

        //Regenerate random x and y positions for the next particle   
        xPos = random.nextInt(size);
        yPos = random.nextInt(size);
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ashsha91
  • 11
  • 5
  • 1
    1) I would replace `image.getWidth()` & `getHeight()` to `int` attributes declared outside the outer `for` loop. 2) Great benefits can be had from calling for the byte array of the image and making all changes directly to that array. 3) For better help sooner, post an [SSCCE](http://sscce.org/). 4) Is this [tag:homework]? If so, it is advantageous to tag it as such. – Andrew Thompson Sep 15 '12 at 00:56
  • 1
    What does your [profiler](http://stackoverflow.com/q/2064427/230513) say? – trashgod Sep 15 '12 at 01:01

1 Answers1

2

Although the implementation of draw() is not shown, it looks like you're updating a BufferedImage and then rendering it.

  1. The first step is always to profile your existing code, looking for easily implemented optimizations.

  2. The second step is sometimes to set aside the existing code and try a different approach.

You may be able to leverage the Mode-View-Controller pattern, outlined here and discussed here. In particular, let your DLA model evolve on a background thread at full speed, while updating your view at a more sustainable rate. This article suggests several approaches to synchronization and includes a related example that uses javax.swing.Timer to pace the updates.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045