Connecting Tech Pros Worldwide Forums | Help | Site Map

Is there something in C++ that is faster than printf()?

Newbie
 
Join Date: Aug 2007
Posts: 9
#1: Oct 6 '07
i wanna know if there is any way to write in c++ faster than printf.
tnx
khosrow

Newbie
 
Join Date: Oct 2007
Posts: 4
#2: Oct 6 '07

re: Is there something in C++ that is faster than printf()?


I don't know if it is faster than printf, but you can use cout in C++ to print something:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.      cout<<"Hello world"<<endl;
  7.      system("PAUSE");
  8.      return 0;
  9. }
  10.  
  11.  
it will come out like:

"Hello world
Press any key to exit the program..."

(the 'endl' comand, means that the program will put a paragraph after that; the 'system("PAUSE")' control is to make the program wait until you press any key. this is in case you didn't knew.)
hope it was usefull.
anoris
Newbie
 
Join Date: Aug 2007
Posts: 9
#3: Oct 6 '07

re: Is there something in C++ that is faster than printf()?


Quote:

Originally Posted by anorisml

I don't know if it is faster than printf, but you can use cout in C++ to print something:
[CODE BEGGINS HERE]
#include <iostream>

using namespace std;

int main(){
cout<<"Hello world"<<endl;
system("PAUSE");
return 0;
}

[CODE ENDS HERE]
it will come out like:

"Hello world
Press any key to exit the program..."

(the 'endl' comand, means that the program will put a paragraph after that; the 'system("PAUSE")' control is to make the program wait until you press any key. this is in case you didn't knew.)
hope it was usefull.
anoris

have you ever tried to submit programs at online judge sites? if you have done that at least once, then you must know that cout is much slower than printf. the same code on PAS has got the AC but on CPP TLE. even when compiled by visual.
Newbie
 
Join Date: Oct 2007
Posts: 3
#4: Oct 7 '07

re: Is there something in C++ that is faster than printf()?


Use the lowest level system calls that handle output on your system?

I *think* that if you are looking for max speed, and trying to get as snuggly with the hardware as possible, you will be wasting time looking at anything ANSI.

System specific low level system calls is what i would look for.

Although i am by no means an expert, or even an intermediate.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#5: Oct 7 '07

re: Is there something in C++ that is faster than printf()?


printf()?? In C++??

printf() is part of the C library and is in C++ only to support relic C-code.

printf() can display only a very limited quantity of material. Like only the built-in types and pointers. Mostly useless in a program using objects.

Besides, it doesn't work for disc files, Unicode, network communciations, none of the user defined types (which is 99% of all code).

The << operator using an ostream can work in all of these evirinments with no code change. You just << to the correct type of ostream.

Otherwise, you printf(), sprint(), fpintf(), vsprintf() , etc..and you still don't have the versatility.

If you don't like the object-oriented << operator, then don't use it. You can still write your own stream class by deriving from basc_stringbuf.

Speed is almost never the issue. When it is, you insert assembly into your C++ program by using the inline assembler. Keep in mind a problem at 1GHz is half a problem at 2GHz and even less if there are multiple cores.
Reply