I'm trying to make a simple little game, but redrawing the game board is a lot stower than I'd like. Any suggestions to optimize my code?
I'm using a Console program because I'm still a novice and am not ready to tackle a real GUI yet.
How slow is it? If it's slow, it's slow because either (1) computing Board[i][j] is slow or because (2) system("clear") is slow or because (3) appending chars to a stringstream is slow.
1 would be the case if you used a bad datastructure for whatever Board is.
2 could be the case.
3 is probably not the problem -- stringstream implementations are generally slow on a lot of systems. They'll acquire a mutex before doing their operation, which means that appending chars is pretty expensive. However, the board is so small that you wouldn't think this to be a problem. There are only about 7500 characters there, so even if each append took 100 ns it would take under a millisecond to build the stringstream.
Gabebord is a char array w/ 124 x 61 elements. When printBoard() is called, the screen begins redrawing almost instaneously, so I know none of the computations beforehand are slowing it down. It's the process of printing each subsequent line to the screen which appears slow.
For anyone else who comes across this thread looking for an answer, I've solved my problem. I used C-style printf() instead of C++ style cout. Despite rumors to the contrary, it really is a lot faster. And here's the reason: cout takes multiple types as parameters and has to convert them; conversly, printf() requires a C-string and type conversions are nonexistent.