I was coding for a particular problem whose time limit was 2 seconds.. The no. of input was max 300000. Simply taking in the number using cin and outputting the same number with cout will take more than 2 seconds.
Any ideas on how to increase efficiency??

What's this input source?
What's the output target?

I'm not sure, but instead of using cout, try placing it directly in the video memory(0x40000000). printf is more time efficient than cout, because in cout the datatype is not mentioned

Simple, Use file I./O.
Or you can also use the input redirection operator '<' while calling the program from the command prompt $ myprogram.exe < input_data.txt

>try placing it directly in the video memory...
30000 numbers per second... in video memory...
May be, 30000 numbers per second directly from the keyboard?...
;)

>try placing it directly in the video memory(0x40000000)
Haha, directly!
I assume the OP isn't using DOS anymore :P

The problem has some 300000 test cases which has to be solved in 2 seconds.
I tried using scanf and printf but still wont happen in 2 seconds

At first 30000 numbers. Now 300000 cases. No answers.
It looks like a farce.

catchmrbharath, can you be more specific about the problem? What is the input source(s)? What kind of processing has to be done? One of the ways I can attempt to find a solution is to play around with it, but I need to know more about the actual problem first before I can setup my own simulation.

>I tried using scanf and printf but still wont happen in 2 seconds
I told you to use file I/O. Use fopen fread.
By the way this is a C++ forum and not a C forum. We usually don't understand printf and scanf.

>By the way this is a C++ forum and not a C forum.
Yes, yes it is. But printf and scanf are a part of C++ as well. It's not uncommon to drop down to stdio when iostreams are too slow.

>We usually don't understand printf and scanf.
Speak for yourself. I don't know many C++ programmers with any real experience who don't understand the basics of printf and scanf. Sure, I wouldn't expect them to have intimate knowledge of the formatting language, but I wouldn't expect that of a C programmer either.

>Yes, yes it is. But printf and scanf are a part of C++ as well. It's not uncommon to >drop down to stdio when iostreams are too slow.
I don't know what made me type that but I think I was in illusion that the OP has posted some C code here.
I don't think the standards says anything about iostream being slower? (Though it usually are (though the difference is insignificant) )

>Speak for yourself. I d.................
It is pretty ovbious that I was trying to discourage him using printfs.
If it satisfies you " My backspace key was not working" ;)

>I don't think the standards says anything about iostream being slower?
The standard doesn't make many performance guarantees. However, while iostreams offer the potential for being faster than stdio, I haven't seen any implementations that take advantage of that. The result is that iostreams are often quite a bit slower than stdio (though it's gradually getting better).

However, while iostreams offer the potential for being faster than stdio, I haven't seen any implementations that take advantage of that.

Probably it's possible on std::streambuf level (for std::filebufs only? ), but apparently most of C++ programmers do not know how to handle this standard stream i/o interface. It's doubtful that it's possible to improve anything for cout/cin streams synchronized with stdout/stdin...

scanf and printf make a lot of difference in speed.. my program using cin ,cout ran for 1.71 sec while the same program with scanf and printf ran for 0.22 sec...
In printf and scanf we specify what variable type it is ,so it takes less time

>In printf and scanf we specify what variable type it is ,so it takes less time
Quite the contrary, we do not specify variable type in printf and scanf (remember int printf(const char*,...) signature) but do specify it (at compile time) in overloaded operator<< and operator>> for streams.

On the other hand a single call of scanf/printf potentially is much more effective than a sequence of independent calls of overloaded stream operators, especially if the compiler supports format string literal parsing at compile time. Optimized printf/scanf implementations make a single i/o internals setting then use a very fast mechanics to fetch variable argument list elements...

If you need something faster, wrap your own crude i/o tools. I'm sure you could get them faster than most provided ones, but not as reliable.

I'm not sure, but instead of using cout, try placing it directly in the video memory(0x40000000). printf is more time efficient than cout, because in cout the datatype is not mentioned

Never heard of that address. Usual most people use (0xb800 << (MACHINE_ADDRESS_RANGE-16))

Never heard of that address. Usual most people use (0xb800 << (MACHINE_ADDRESS_RANGE-16))

You are right, the video memory is actually 0xB800, it just didn't quiet come to my mind.

Just take a crude printf code and clip off the formatting and other unnecessary things, and you'll get a more efficient code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.