Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code efficiency question 1

Status
Not open for further replies.

WebDrake

Programmer
Sep 29, 2005
106
PL
Does anyone have any comments on which of these pieces of code will run more efficiently?

In these code examples, [tt]check[/tt] is a variable of type [tt]bool[/tt] (it is an actual variable, not an expression that is reevaluated each time the [tt]if()[/tt] statement is run), and [tt]i[/tt] and [tt]N[/tt] are unsigned ints.

Code:
if(check) {
    for(i=0;i<N;++i) {
        function_1();
        function_2();
    }
} else {
    for(i=0;i<N;++i)
        function_1();
}

and

Code:
for(i=0;i<N;++i) {
    function_1();
    if(check)
        function_2();
}

I'd be inclined to go with the former but I'm interested in what more experienced people have to say. Many thanks! :)
 
former
the test for check is not executed N times
 
Be careful.

Those two ways are not equivalent. If check changes its value, the result will vary.

I don't think it's a performance question, they are two different functionalities.

Cheers,
Dian


 
Diancecht said:
Be careful.

Those two ways are not equivalent. If check changes its value, the result will vary.
I'm aware of that. For the case I'm concerned with, [tt]check[/tt] remains at a single value throughout either option.
 
Nice, now consider a big N, how much time can a boolean comparison take, microseconds maybe, you could improve a few milliseconds, and you have a code less elegant and probably hard to maintain.

Cheers,
Dian
 
Diancecht said:
Nice, now consider a big N, how much time can a boolean comparison take, microseconds maybe, you could improve a few milliseconds, and you have a code less elegant and probably hard to maintain.

Hi Dian,

Thanks for the thoughts. For what it's worth, I am talking about having very large numbers, so little savings are often important.

In the case of this particular loop, N will not be large; but the whole thing will be contained in a bigger loop that will go round on the order of 100 million or 1 billion times. Just to give you some idea of the numbers involved.
 
Ok. So if the function needs 100 msec to execute, your program will last 3 years. And you saved one day with your if. Usually performance has to do with algorithm selection, data structures and so on.

When you get to these kind of levels, I think there's no worth going on. You will have really little savings and you have the risk of a logical mistake that will result in a real loss of time.

Cheers,
Dian
 
I feel you're probably barking up the wrong tree here.

Which approach is most efficient probably depends on the context of your code and on the processor running it. A modern processor will predict jumps in advance; under some circumstances a jump that always goes the same way will take no cycles at all. So you may find in practice that there is no difference (really no difference) between the two versions.

If, on the other hand, you've embedded it in a chunk of code that's so big it won't fit in the processor's cache, you won't have any pipelining and you'll have cache-misses and all sorts of horrible things going on.

But frankly Diancecht is right; you can usually shave a few
percent off total execution time by spending months thinking about the compilation and cycle times of a handful of instructions. You probably made more difference when you chose which compiler to use. And you can change the speed of operation of the whole process by orders of magnitude by simple things like choice of algorithm and aligning of data.

Just to point out the way things can go wrong: if you really need to save time, you haven't mentioned the possibility of having a single function call that does the job of both function1 and function2, and calling it if check is true. This saves you a whole function call, which is (probably) vastly more expensive than testing a boolean that goes the same way every time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top