As you don't use real formatting, and print only individual chars you can use the simple putchar() function:
for (y = 0; y < MAX_Y ; y++)
{
putchar ('\t');
for (x = 0; x < MAX_X; x++)
{
putchar(base[y][x]);
}
putchar('\n');
}
On my machine, iterating the loops 100,000, testing it in 3 runs and redirecting output to /dev/null, gave me:
- 6.679u, 6.663u and 6.766u with
printf,
- 3.309u, 3.315u and 3.312u with
putchar,
- 0.263u, 0.261u and 0.263u (with
putchar_unlocked).
If I use terminal output I have:
- 8.153u with
printf,
- 3.862u with
putchar,
- 0.634u with
putchar_unlocked.
Elapsed times are:
- 0:09.46 with
printf,
- 0:07.75 with
putchar,
- 0:05.06 with
putchar_unlocked.
-Edit----single write---------
You can also group everything into a single string and use puts, like this:
char baseString[MAX_Y*(MAX_X+2)+1];
int p = 0;
for (int y = 0; y < MAX_Y ; y++)
{
baseString[p++] = '\t';
for (int x = 0; x < MAX_X; x++)
{
baseString[p++] = base[y][x];
}
baseString[p++] = '\n';
}
baseString[p] = 0;
puts(baseString); // or fwrite(baseString,p,1,stdout);
In that case, test gives:
- 0.448u, 1.155s and 0:04.99 (puts)
- 0.418u, 1.077s, 0:04.81 (fwrite)
This is slightly better than putchar_unlocked.
All test made on OSX 10.9, Intel Core i7 2.3Ghz.
printfis as old asfor(at least in C)...