4

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.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
Lauer
  • 517
  • 1
  • 6
  • 11
  • 3
    I would think `WriteConsole` would be faster, seeing as how it's a direct winapi function, but I haven't heard/read anything on the subject. – chris Jun 19 '12 at 22:37
  • 4
    Why? Is this the bottleneck of your code? If so, you're printing way too much data to the console. – Adam Rosenfield Jun 19 '12 at 22:39
  • I like to make my basic classes as efficent as possible so that i dont need to modify them later. im just coding for fun, so there is no real problem. – Lauer Jun 19 '12 at 22:41
  • 1
    I'd be very surprised if you managed to write anything that you needed to modify later. The fastest way to do it would probably be to arrange to memory map part of the console and then do a memcpy or 0 copy write to that buffer. That's crazy though! – Flexo Jun 19 '12 at 22:45
  • What do you mean by "iostream type class"? What's wrong with `cout`? If efficiency is as important as you claim why use iostreams instead of a lower-level API? – Jonathan Wakely Jun 19 '12 at 22:49
  • possible duplicate of [cout or printf which of the two has a faster execution speed C++?](http://stackoverflow.com/questions/896654/cout-or-printf-which-of-the-two-has-a-faster-execution-speed-c) – Jon Cage Jun 19 '12 at 22:52
  • 1
    If you want raw speed, you'll probably have to bypass the formal, standardized mechanisms and dig into very low-level mechanisms . However, your post reeks of premature optimization which is often a major cause why people end up having to rewrite their code later, not because it wasn't fast enough in the first place. You should seek correctness first above all, and chances with something like console I/O are that your correct solution won't even need to be optimized. – stinky472 Jun 19 '12 at 23:00
  • @Johnathan Wakely: i like the formating of printf("a is:%i, b is %i and c is %i\n",1,2,3); but it cannot be made to use custom types. cout<<"a is:"<<1<<"b is:"<<2<<"c is:"<<3< – Lauer Jun 19 '12 at 23:05
  • Thanks for the insignt, i will use printf. – Lauer Jun 19 '12 at 23:06
  • noob question: since i have my answer, how do i close the thread? – Lauer Jun 19 '12 at 23:07
  • Just click on the grey tick to the left of my answer if that's the one you're happy solves your issue.. – Jon Cage Jun 19 '12 at 23:41

3 Answers3

4

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.

Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59
1

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:

  1. Get something the functionally performs.
  2. Set up a benchmark to test whether your solution is fast enough.
  3. If it's not fast enough, try something else then go back to 2.
  4. If it's fast enough you're done!

It sounds like you've not even started designing / coding from your question. Beware premature optimisation...

Jon Cage
  • 36,366
  • 38
  • 137
  • 215
  • after spamming "PRINT" to the screen 20,000 time i see what people mean. i will go with printf since it only took 5 seconds – Lauer Jun 19 '12 at 22:58
1

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).

ARandomFurry
  • 203
  • 3
  • 9