Fast i/o function implementation [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was searching net about fast i/o in c++ for various contest and i found one piece of fast input function . But i am just a beginner in c++ and couldn't implement it to a simple programme to input using that function . So if someone can give an example code like to input a variable using that function , it would really be a help to me . Here's is the function i found :-
inline void fastRead(int *a)
{
register char c=0;
while (c<33) c=getchar_unlocked();
*a=0;
while (c>33)
{
*a=*a*10+c-'0';
c=getchar_unlocked();
}
}

Do not worry about the speed of your I/O until the speed of your I/O is a problem your program is having. Doing pre-mature optimizations, especially if you do not understand why you are performing a optimization, will likely either cause your program to run slower or the amount of time it took to find the optimization will be longer than the total amount of time saved the optimization does over the lifetime of your program due to the fact that what you thought you needed to optimize was not what was causing the program to be slow.
Stick with easy to read and maintainable code and go back and do things like optimized I/O once you have profiled your completed application and you find I/O to be the real bottleneck

Related

How do I speed up C++ execution with in-line functions? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am transferring a FORTRAN Monte Carlo program to C++ and found that when completely transferred the C++ program runs nearly twice as slow as the FORTRAN program. I am trying to draft a 2nd version of the C++ program where many of the functions are brought in line through the use of class structures to speed things up; however, some of the functions are upwards of 40 or 50 lines and I have read that bringing large functions in line can slow down the program. What is too large when it comes to bringing functions in line and how can I speed up a C++ program (without multi processing) such that a C++ program can execute as fast, or near as fast as a FORTRAN program?
Inlining in C++ is only a suggestion to the compiler. If the function is too complicated, it will not be inlined by most modern compilers. The compiler will do what it can to optimize the calls in any case, even without the "inline" keyword, so long as the implementation is available when it's being compiled. There are also compilers that will inline across compilation units, but this is less common.
In any case, it's unlikely that function calls are dominating your processing time. You probably want to profile your code to figure out where the bottleneck is actually at before putting too much effort into micro-optimizations that the compiler is probably doing for you in any case.

How does one make something real time? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This might be a really stupid question, but I can't find any info on this, and I can't find out what to search for.
How do you make something real time? For example, say we have a game, how does it run continuously without us calling a tick() method at different parts of the program?
If you are developing the program to run at a time in particular the best way I know of is to develop it to run at a particular loop rate using timed delays.
IE:
while(forever) {
do something
test how long it took
take up the remainder amount of time for the loop to run at a rate (ie 100Hz)
}
If you are desperate for real time applications you can develop and use QNX:
http://www.qnx.com/
But this certainly wouldn't be a good environment for programming games.

Sequential and running in one thread time difference c++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Here is my source code for testing multithread performance in C++. Please tell me why is time about 5x smaller for ONE thread running(WaitForMultipleObject()) then first sequential performance. I expect almost same result for sequential performance and running with only one thread. Thanks
http://pastebin.com/EeJ5qW03
OS will decide when will your thread start running and it will also decide if there's a need for dispatching, perhaps. Add to that, it also has to create a separate stack for your thread, perhaps.
Read about the overhead on thread creation. All in all, the overhead is system specific.

Various types of compiler optimisations? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have come across loop-unrolling but what other types of compiler optimization are there for C++ code?
If possible, I'd be interested specifically for the Intel Compiler and GNU Compiler.
If I could obtain a list I can google for the explanation upon each type of optimization.
if you are talking generically, beyond loop unrolling, there is also the basic:
remove unchanging variables out of a loop.
optimizing away unused but initialized objects/variables/instances.(dead code removal)
expanding function calls in line, like strlen();
using processor specific directives/commands.
thats off the top of my head... I will be back with some scientific (wikipedia lol) answers
heres more:
5. static variable inlining
6. complex branch optimization
ok, tired lol heres a decent link i was just looking at :)
http://www.eetimes.com/electronics-products/embedded-tools/4086427/Advanced-Compiler-Optimization-Techniques

Generating word library - C or C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I need to create a simple application, but speed is very important here. Application is pretty much simple.
It will generate all available chars by saving them to text file. User will enter length that will be used for generating so the application will use recursive function with loop inside.
Will C be faster then C++ in this matter, or it does not matter?
Speed is very important because if my application needs to generate/save to file 10 million+ words.
It doesn't really matter, chances are your application will be I/O bound rather than CPU bound unless you have enough RAM to hold all that in memory.
It's much more important that you choose the best algorithm, and the best data structures to back that algorithm up.
Then implement that in the language you're most familiar with. C++ has the advantage of having easy to use containers in its standard libraries, but that's about it. You can write slow code in both, and fast code in both.