Reputation: 517
I want to make an iostream type class. I would like to find the most efficient way to write a set of characters to the screen. Ideas:
printf-I dont want the type formating I need to do that myself.
WriteConsole-Read that it was slower than printf? True/False?
*Assembly-Dont know how
other?
*my main concern is if I could find how to do it. I dont have any rush as far as time.
EDIT: for some reason WriteConsole is slower.
Upvotes: 4
Views: 7503
Reputation: 203
I found that for Windows using WriteConsoleOutputCharacter()
averages about the same as fwrite()
for stdout
, and requires one less file to include if you're not using <stdio.h>
. Both are very fast though. I did not test FillConsoleOutputCharacter()
. I probably didn't use that great of a benchmark either. As for premature optimization I had to tackle this problem first when creating a cool little library for the console window that more or less turned it into a windows based environment with an overarching system managing it. I used this system for college and personal text based games. For logging and similar behaviour using cout
and friends does the job just as well, despite being slow(er).
Upvotes: 1
Reputation: 11499
Use "fwrite":
fwrite( buffer, size, 1, stderr );
This will be much faster than you will ever need. And you have a bonus that you can then make your iostream class be able to write not just to the console but to files too.
Upvotes: 4
Reputation: 37488
I would suggest trying a few methods (you've mentioned a couple there) and benchmarking the results. You may be suprised by your results but even if they're as you expect, you can at least be certain you're doing the best you can. For the record though, I would be surprised if you find much faster than printf.
The most pragmatic way to code (in my experience) goes along these lines:
It sounds like you've not even started designing / coding from your question. Beware premature optimisation...
Upvotes: 1