Navigating arrays in C (performance)? [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.
Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?
How does the compiler treat both approaches?

Any modern compiler should produce an equivalent assembly code for both methods.

I did a short test. I created int arr[10] and set all cells to 10 using normal for loop indexed by int and one using int*.
What was strange form me (I accept Midhun MP arguments) pointer indexed loop assembly code was larger then normal approach (1 line more). But when I turn O3 optimization output was exactly the same.
IMO code should be easy to read and work without errors on the first place. Micro optimization can be done only if you really need them. In other cases readability beats performance.
If you are curious how it works. Just do this test yourself. Prepare 2 versions of code, compile it with gcc -S and diff outputs.

Related

Implementation of vector of integers with overloaded operators, fails to run properly [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 have huge problems with making this code work properly.
http://pastebin.com/Mi6gj188
There's an output from example program on the bottom. It simply crashes and doesn't deliver proper results too. It seems that none of the overloaded operators work as it should
You didn't write a copy constructor, or use RAII. As a result, every time your vector object is copied (and it is, a lot, because you make no use of references!!) your internal data pointer is copied, sharing it amongst multiple objects (each of which will attempt to delete it on destruction) causing a horrible bug.
Your book tells you about the rule of three, which you should now go ahead and work on following.

remove or replace duplicated C++ code in one function on Linux [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 am working on C++ on Linux.
I need to remove some duplicated code in two functions.
One function is for computing and another one is for logging.
There are some code that are duplicated in logging(), which is much longer than computing().
The duplicated code is distributed in logging() separately, which means that they are not just copy and paste from computing().
I need to figure out the duplicated part line by line, remove them and then replace the necessary results by passing them as parameters from computing() to logging.
Are there some efficient ways to handle this ?
Look at the functions side by side, identify common blocks of code, then factor these common blocks out into separate methods/functions.
It might not be worth merging them. If you really have to, though, perhaps one common function with an extra bool do_logging parameter.

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.

What are your techniques of optimizing your code in 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 12 years ago.
What are your techniques of optimizing your code in c++?
Write code
Profile code
Tweak performance hot spots
If still not fast enough, go to step 2
1) Write the cleanest and most straightforward code I can.
2) Use a modern compiler with optimized settings.
3) Be done.
Optional:
4) If I think something is noticeably slow, profile my application.
5) Use my profile results to find out what's slow, and fix it.
6) Make sure it's still as clean and straightforward as possible.
7) Be done.
The most important technique is not optimising until you know it's a bottleneck.
Follow coding standards. Have a look at http://sourcemaking.com/