473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

9 New Member
i wanna know if there is any way to write in c++ faster than printf.
tnx
khosrow
Oct 6 '07 #1
4 4004
anorisml
4 New Member
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
Oct 6 '07 #2
khosrow
9 New Member
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.
Oct 6 '07 #3
32Alpha
3 New Member
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.
Oct 7 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Oct 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: andrea_gavana | last post by:
Hello Simo & NG, >Correct me if I'm wrong but since it _seems_ that the listing doesn't >need to be up-to-date each minute/hour as the users will be looking >primarily for old/unused files, why...
17
by: steve | last post by:
I am writing for game application. Performance is an issue. Any advise would be appreiciated.
54
by: zhaoyandong | last post by:
In one of my interview, some people asked me why C is faster C++, and tell me to illustrate at least two reasons. I can't find the answer in the web. I'll appreciate any suggestion on this....
104
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
5
by: Donkey | last post by:
Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below: #include <stdio.h> const long double...
5
by: sololoquist | last post by:
#define COUNT_UP #include <stdio.h> #define N 10 int main() { int i; #ifdef COUNT_UP for (i = 0; i < N; i++)
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
48
by: istillshine | last post by:
When I used gprof to see which function consumed most running time, I identified the following one. sz was less than 5000 on average, but foo had been called about 1,000,000 times. I have tried...
36
by: lak | last post by:
Which is faster? Post Increment or assignment? Why? I was not able to get any things.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.