Keilly: Speed up file writing

Thursday, June 28, 2007

Speed up file writing

Simple writing to a file can be achieved by the following:

widget.system("/bin/echo name=" + value + " >> "
+ dataFileName, null);

New lines aren't allowed with this version of the echo command, so only one line can be written at a time. The problem is that system calls aren't very fast: who knows why, perhaps a new shell is created internally each call? Anyway, writing a file of more than a couple of lines is noticeably slow to the user. So we need a way to do this with less system calls.

'printf' does the job perfectly as it allows multiple line writing. Build up the string to write, separating lines with the '\n' character. Then write the whole file in one call....

var dataString = "";
for(i=0; i {
dataString += data[i].name + "=" + data[i].value + "\n";
}
if (dataString.length > 0)
widget.system("/usr/bin/printf '" + dataString +
"' > " + dataFileName, null);

So... much... faster...

No comments: