Printf is not buffered

It seems that each time Printf is invoked, it outputs directly to the Output window, forcing the Output window to process the new data, without buffering.

This is problematic when printing out hundreds of thousands of short strings (couple chars long). It takes much longer than manual buffering, and it freezes the UI too.

The behavior is observed in a template. I didn’t try a script.

Compare:

for (int i=0; i<100; i++) {
  Printf("%i: ", i);
  for (int j=0; j<1000; j++)
    Printf(" %i", j);
  Printf("\n");
}

with

local char buf[100000];
local int bufpos = 0;
local string out;
local int outlen;

for (int i=0; i<100; i++) {
   bufpos = 0;
   for (int j=0; j<1000; j++) {
      SPrintf(out, " %i", j);
      outlen = Strlen(out);
      Memcpy(buf, out, outlen, bufpos);
      bufpos += outlen;
   }
   buf[bufpos] = 0;
   Printf("%i: %s\n", i, buf);
}

The latter is much faster.

Printf needs buffering just like the C printf has deep in the guts of the stdio code. If the buffer doesn’t overflow, it can be flushed on a timer so that the Output window stays updated even if sporadic short output is all there is. The buffer can probably be fairly large, like several kb, so that the UI is not bogged down with updates from frequent short Printfs. If that buffer size could be configurable in workspace settings, so much the better.

Yes, you are correct that Printf does not work well when there are a large number of updates. We will get this improved in a future version.

Graeme
SweetScape Software

1 Like