Does this idea regarding loops make sense?

0 Dol Lod · March 17, 2015
I was always thinking that for loops can be extremely inefficient b/c of the constant condition checking. Thinking about it this way means that hardcoding would be a better idea to improve performance, but then the code would become hard to read and hard to change. So what I was thinking about was writing a program that would take a  file,scan for loops, in particular for loops that could be replaced with hard coded replacements. Then, I get all the efficiency I want. I was thinking that for safety, I could only perform this kind of optimization for const variables. Let me show an example of what I mean.

const int size=3;
int position;
int count=0;
for(position=0;position<size;position++)
            count++;
//This whole for loop would be directly replaced by count++;count++;count++; instead. 

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · March 17, 2015
So these kinds of optimizations are already performed, then. Thanks for the information.  
0 Dol Lod · March 18, 2015
Thanks for the link. I did read up on it properly looking for the segment you were referring to. If there is a flag that already does this, there really is no need at all for this kind of optimization

To clarify about what I was saying, I am not going to meddle with the actual compiler. Instead, I'm aiming to actually replace the code itself with a hard-coded version of what the code would do. For ex., if I took in a cpp file and it contained a for loop like what I showed above, it would scan the C++ code, output the file with a new name. Then, I could compile that file instead.

The main purpose will just be to remove conditional statements b/c conditionals can be very expensive in terms of  computations. Explanation: http://stackoverflow.com/questions/315306/is-if-expensive

So basically, I was aiming for is something like this:

original code->optimized code->compiler,->binary

Not what I was aiming for:
not original code->my compiler->binary 
  • 1

C++

107,198 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator