Bug? print() to console slows sketch down

I’m doing a lot of printing to console and for sure it’s obvs not really that necessary beyond debugging. I was very surprised to find out that it was slowing a sketch down enormously! Perhaps that’s my naivety but it seems like a bug?

Are there any general resources for a processing noob regarding sketch optimisation?

Many thanks!

I think println is slow but also buggy

when you print a lot it might crash your sketch

So really don’t use it, only for debugging of course

To produce text, better use saveStrings - see reference

1 Like

Thanks Chrisir :slight_smile:

1 Like

Yeah, print statements are slow. Google “system.out.println slow” for an explanation about why.

One thing you can do is limit how often you’re printing. For example you might only print once per second instead of every frame:

void draw(){
  if(frameCount % 60 == 0){
    println("hello");
  }
}
3 Likes

Well - bugger me! Thanks Kevin. Curious stuff.

No – it would be nice, although optimisation / optimization tends to depend a lot on what you are doing. For example, 1) printing to the console can be slow 2) the first call to text() can cause lag, on that frame only 3) there are memory efficient ways of dealing with frame buffers if you are trying to time-delay video … and if you are doing trig, it helps to pre-compute expensive parts that you re-use many times… but, in general, there are so many different ways to optimize that it depends a lot on what kind of sketch it is.

So, just ask for help on individual optimization tasks. Many of them people have seen before. If you really want to get deep into it, I would recommend a general survey of the underlying language features (in this case, Java) such as:

1 Like

Hey Jeremy —

Thanks for taking the time to respond. I’m self taught from a very young age so have lots of sloppy processes in me. My question was always: “but does it float?” I never much cared for efficiency till recently. I have started also to admire the aesthetics of elegant code as much as benefits in performance.

Thanks again for your reply. I’ll surely ask more questions :slight_smile:

LNP

1 Like

Great! Many here are self-taught, in whole or in part, and we are all still learning. When working on a project basis, optimization tends to become more interesting once it is needed (which often seems the right way to prioritize it). Some design patterns and best practices come with time and experience.

Looking forward to them!

1 Like