• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Faster C++ screen output

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

JigPu

Inactive Pokémon Moderator
Joined
Jun 20, 2001
Location
Vancouver, WA
Is there anything quite a bit faster at screen output than 'cout'? Or some way of optimizing how I write to the screen to result in faster outputs?

I've ported over my BASIC prime number program to C++, and it's faster like many people have told me until you make it write to the screen. The writing algorthm used in 'cout' just plain sucks compared to BASIC's 'print'.

Benchies (so you know just how much faster I need to write):
BASIC - 1 to 1 Million (to screen) - 3.571 seconds
C++ - 1 to 1 Million (to screen) - 9.7 seconds
C++ - 1 to 1 Million (no output) - 2.2 seconds

Anything you C++ gurus know of that will speed it up writing to around my BASIC's time?

JigPu
 
Well, yeah printf from stdio.h should be more efficient than cout. I never use iostream unless I absolutely have to. I don't know if you'll see _that_ much improvement though.
 
How do you get printf to work with a long integer? The complier complains to me whenever I use the line

printf(test_prime);

It says: "Could not find a match for 'printf(long)' in function main()"
Huh?

JigPu
 
JigPu said:
How do you get printf to work with a long integer? The complier complains to me whenever I use the line

printf(test_prime);

It says: "Could not find a match for 'printf(long)' in function main()"
Huh?

JigPu

With printf you have to use output formatters to tell the function how to interpret the variable. Usually it's of the format:

printf ("%i", var);

%i means integer. It might work for a long. If not you could try %l. If neither of those works, you might have to google around for printf format specifiers.
 
Here's a list of the conversion characters for printf. These should all be preceeded by a % :

Code:
d , i      int, signed decimal notation
o          int, unsigned octal notation (no leading zero)
x, X      int, unsigned hexadecimal notation
u          int, unsigned decimal notation
c           int, single character, after conversion to unsigned char
s          char *; characters printed until a '\0' is reached
f           double
e, E      double
g, G      double
p          void *; print as pointer
%         no agrument converted, print a %

Between the % and the conversion character you can place an l or an L (lower and upper case Ls). Lower case indicates the argument is a long or unsigned long, while uppercase means the agrument is a long double.
 
Thanks dudes! It's still not as fast as I'd like, but it did shave off 2 seconds (down to 7.6)! I've googled around for other output methods, but I can't seem to find any, so I guess I'm SOL there.

Also, I'd like to thank you for explaining how printf() works. I never could figure out very well how it used. I see it all the time in source code on the net, but it's such weird syntax that my brain dosen't deal too well with it :D That's why I use COUT if I can :)

Thanks a ton!!!
JigPu
 
You're going to see a slow-down anytime you do I/O no matter how efficient the flavor of print you're using is.

How often are you writing to the screen? If it's every iteration, you can always consider only printing every 1000 iterations. You could also consider storing your iteration values to an array in memory, and then just dumping the array to screen or file when you're done.
 
NookieN said:
You're going to see a slow-down anytime you do I/O no matter how efficient the flavor of print you're using is.

How often are you writing to the screen? If it's every iteration, you can always consider only printing every 1000 iterations. You could also consider storing your iteration values to an array in memory, and then just dumping the array to screen or file when you're done.

I think this is the best suggestion I hear so far. It is true that if you output to the screen, that will be the slowest part of the process. Screen output is terribly slow, no matter what high level language you use (C++, Java, C#, etc...). Storing the values your program generates in an array, then outputting to the screen when all is said and done should be the fastest, but will still tack on a some time.
 
Well, I'd do staggered output (store 1,000 primes; dump to screen) or total ouput (store all primes; dump to screen), except that it doesen't work too well for the real-time output I want.

I'll probably throw that in once I get around to writing a version that uses an .INI file so that users could custimize settings based on how they run it I would do it.... :D


Though I still wonder why C++, one of the fastest languages around has such slow screen output compared to one of the slowest languages around :D Kinda ironic don't you think? :D
JigPu
 
Try to find a curses library, like ncurses if you're on Linux or pdcurses or zcurses if not... that might speed things up just a bit.
 
Back