How do I speed up C++ execution with in-line functions? [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 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.

Related

Why the OS allocates more memory than deemed necessary for a small sized executable? [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.
Ok so I wrote this minimal C code and complied it to an executable in release mode
1.void main()
2.{ for(;;); } //this is here to make the app hang.
The executable file size itself is 6kb, I didn't include any headers. Even if the entire exe file gets copied to the ram, apparently it shouldn't occupy more than 7 kb, nevertheless the OS allocates 320 kb, why is that? I'm using windows.
It seems like you're confused and mixing a wide variety of concepts. Let me try to explain:
That program is very clearly an infinite loop which explains why it doesn't end (or what you call "hang").
The compiler/linker still need to write a valid executable for your OS, and this involves a bunch of headers and stuff, which could be easily consuming 6kb.
320kb in a mainstream OS at this point in time seems like almost nothing and it can mainly be OS overhead. It is hard to say more without knowing what OS.
I strongly encourage you to disassemble your code. Another option is to play with your compiler options to optimize for executable size. I think the bottom line is that you're expecting that since your program doesn't do anything useful its size should be zero, and this is an unreasonable expectation.

Fast i/o function implementation [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 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

fast on-demand c++ compilation [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'm looking at the possibility of building a system where when a query hits the server, we turn the query into c++ code, compile it as shared object and the run the code.
The time for compilation itself needs to be small for it to be worthwhile. My code can generate the corresponding c++ code but if I have to write it out on disk and then invoke gcc to get a .so file and then run it, it does not seem to be worth it.
Are there ways in which I can get a small snippet of code to compile and be ready as a share object fast (can have a significant start up time before the queries arrive). If such a tool has a permissive license thats a further plus.
Edit: I have a very restrictive query language that the users can use so the security threat is not relevant. My own code translates the query into c++ code. The answer mentioning clang is perfect.
Running Clang in JIT mode should provide the speed you need, and example can be found here, safety on the other hand is something else...
Ch also had a JIT added, and seeing as its an interpreter, it might provided an easier sandboxed/controlled environment.
In addition to Necrolis answer, there's also specialized C++ parser Cling. Might come in handy.

Matlab computation Vs 'C/C++' computation..Which one is efficient? [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 have some mathematical algorithms implemented in Matlab. I have implemented those algorithms in C++ (i used Microsoft VS 2005). When i compare matlab code output with C++ code output, it is 98 to 99% matches with matlab output. Shouldn't it be 100% matched?
Is the matlab computation efficiency is better than C/C++?
Generally, no, Matlab will not produce more exact results just because it is Matlab. However, there is a number of things that might make a difference:
Different implementations of the same algorithm might have been written with different numerical stability in mind.
C and C++ compilers generally allow you to set compilation flags to do fast math, which changes floating-point math behaviour.
The output options for floating point numbers might just differ, making results look different.
The Matlab and C version might have used different floating point precisions.
In matlab also there would be appropriate compiler so in this scenario its hard to say
that matlab computation efficiency is better than C/C++
if your code is same in both case then there should be same output. If you found some differences between them them its should be because of their compiler version difference.

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.