Fastest way to output

Hi,

I have a function which returns a 2d array. I want to print this 2d array out into a file (on one line). I can obviously loop through and print with an ofstream object but this is very slow (and this function is called a lot and is critical to my application's performance).

Is there a faster way (e.g. concatenating the char array into a std::string and then printing)?
I don't think there is a direct method . But there can a be a different alternative , use standard outout (fast ones like printf() not cin , and fastest like getchar_unlocked) and call your file externally from a batch file executing batch command to write output produced to file (.txt or .*).

Example of batch if you are unaware is (for windows) :-

@echo off
rem run tests
main.exe < 1.in > 1.out

where main.cpp is your src code which compiles to main.exe , you may remove 1.in if you dont want to feed input.
(Note i gave this external solution as you said you are working in an application , so i assumed its not contest or anything similair)
A while ago I was playing with "the game of life" http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
and wanted to display successive generations of cells. The difference between using cout and printf was dramatic, it made an very significant difference.

In that case I was dealing with screen output, but I've also seen similar increases in speed using disk file output too.

If I was dealing with a lot of single character values in an array, I'd be tempted to add a null terminator and printf or puts an entire row as a single c-string. That's assuming there are not zero-bytes as valid data.
Topic archived. No new replies allowed.