Is hardware acceleration possible within the Processing program

Is hardware acceleration possible within the processing program?

  1. For example, when ‘exe’ file is executed, ‘CPU’ and ‘RAM’ are used.

  2. Looking at the ‘framerate()’ function, it seems that the ‘CPU’ quota change between framerate(60) and framerate(1) is different.

  3. When a program repeats dynamic and static things like this, which method is better?

  • Is the noLoop(), loop() method better?
void setup() {
  frameRate(10);
}
int pos = 0;
void draw() {  
  background(204);
  pos++;
  line(pos, 20, pos, 80);
  if (pos > width) {
    pos = 0;
  }  
  noLoop();  
}
void mousePressed(){  
  loop();  
}

(Summary) I want to reduce CPU usage as much as possible when the program is static. When the program is dynamic, I want to increase the CPU usage as much as possible.

If there is a good way, please help. Please tell us.

Leave the frameRate at the default. When you’re animating, it will run at your monitor’s refresh rate (or is it locked to 60 fps? I’m not sure.) When you want to pause or just show a static image, use noLoop(). If you want to update a static image from a key press or mouse action, call redraw() to trigger another call to draw(). If, and only if, you want to start animating again, call loop().

2 Likes

@scudly

Thank you for answer.

The program depends on the situation

  1. There are cases where it has to be turned on 365 days a year
  2. There is a case where it is raised for a few hours.

The same situation now is when it has to be turned on 365 days a year.
Therefore, it shows how to reduce the burden on the CPU through noLoop() and loop().

Is there any better way?

What should happen when it is “turned on”? Is the program drawing anything? Or is it just waiting for a key or mouse or network event? If it is animating, then you would want it to be looping. If it is just waiting for external events, then you could call noLoop() and it should be using very little resources.

frameRate(1) would call draw() once per second. I don’t think Processing goes below that, but you could use a Java timer (which I’ve never used, so I don’t know how to do it) to call redraw() only every minute or hour or once a day if you wished.

1 Like

@scudly

you’re right.

‘mousePressed()’
Like a function, I want to make a function that ‘calls’ when an event occurs.
But I don’t know how.

for example,
The executing function in void draw(){ } is stopped,

  1. How does the program happen through a timer?
  2. Alternatively, it would be nice to be able to create a separate calling function like the ‘mousePressed()’ function.

Use your favorite web search engine to read about “Java Timer”. This example calls draw() every 3 seconds. The TimerTask object’s run() method is called based on how you set it up using schedule() which takes an initial delay and the time period between runs in milliseconds as a long int. If you wanted draw() to run every four hours, you would give the time period as 436001000L.

If you want to change the period between calls, there are other ways to schedule a Timer. Look at Oracle’s Java documentation for what you need. For instance, instead of setting a recurring timer like schedule() does, you could compute the next update time in the task’s run().

import java.util.Timer;
import java.util.TimerTask;

class DrawTask extends TimerTask {
  void run() {
    redraw();
  }
}

Timer timer;

void setup() {
  size( 600, 600 );
  noLoop();
  
  timer = new Timer();
  timer.schedule( new DrawTask(), 0L, 3*1000L );
}

void draw() {
  background( random(255), random(255), random(255) );
}
2 Likes

@scudly

Dear scudly

import java.util.Timer;
import java.util.TimerTask;

class DrawTask extends TimerTask {
  void run() {
    redraw();
    check_func();
  }
}

Timer timer;

void setup() {
  size( 600, 600 );  
  timer = new Timer();
  // timer.schedule(  function, starttime, times );
  timer.schedule( new DrawTask(), 3*1000L, 3*1000L );  
}

long EA = 0;
void draw() {
  EA++;
  background( 255 );
  fill(0);  textSize(50); 
  text(str(EA),int(width/2),int(height/2));
  noLoop();  
}
void check_func(){  
 println(" OK!");   
}

Thank you. It’s a really nice feature.

I think it will be of great help to me.

However, I have one question. When I use that timer, will the overflow part be resolved?
For example,

// EXAMPLE --------------------//
int timers = 0;
void draw(){
if(timers < millis()){
timers = millis() + 1000;
// overflow
}
}
//------------------------------------------------/ /

// Does this timer overflow?
timer.schedule( new DrawTask(), 31000L, 31000L );
//------------------------------------------------/ /

I’m not sure what you are asking.

The EXAMPLE code that you show would be a different way to create a timer but which depends on the draw() function running continuously. That would use up more CPU which is not what you want.

If you have a Java Timer call redraw() and use noLoop(), then draw() will not be called at all until the timer triggers. Using the Java Timer, you would remove the code you have below the // EXAMPLE --- comment and just include in draw() whatever you want Processing to do each time the timer goes off.

Put the noLoop() in setup(). You don’t need to call it each time draw() runs.

1 Like

@scudly

Thank you for answer.

I know what you mean.
I know I only need to write noLoop() once in setup.

Thank you for your kind reply.