Home > front end >  How bad is it to re-evaluate an unchanging condition within a loop?
How bad is it to re-evaluate an unchanging condition within a loop?

Time:12-10

I am writing a .y4m video generator in C that takes all the .bmp images in a directory and writes them to a .y4m video file (after appropriately converting the colours to YCbCr format).

One of the command-line options I am allowing to be specified is whether the .bmp files should be deleted during the video generation. Right now, this happens right at the end of the program, but it would be best for it to occur as images are written (to not increase disk space usage by more than 1 frame at at a time, since the .y4m files are uncompressed video, so can get pretty big).

Thus, if the delete option is specified, the deleting should take place within one of the 5 main loops I have (there is one for each colour-subsampling scheme). The loops have lots of code within, however, so I really would like to avoid duplicating them.

In summary, even though this:

if (delete) {
    while (bmps_remain) {
        // do lots of funky stuff
        remove(path_to_single_bmp);
    }
}
else {
    while (bmps_remain) {
        // do the exact same funky stuff as above
        // ... but do not delete bmp file
    }
}

... is better than this:

while (bmps_remain) {
    // lots and lots of code
    if (delete)
        remove(path_to_single_bmp);
}

... how much of a difference does it really make, and how frowned upon is it to opt for the second option (taking performance into account as much as possible), given that the second option re-evaluates the (unchanging) condition during each iteration of the loop?

Even though it would probably get compiled into some kind of cmp instruction (probably followed by a kind of jnz) which would only take a fraction of a second to perform, this situation occurs commonly in programming, so I would be interested to hear people's opinions.

Thanks.

P.S. The first option would produce a lot of code duplication in my case (and I would prefer to not stick everything into functions, given the previous layout of the program).

CodePudding user response:

The remove( path_to_single_bmp ); instruction is several orders of magnitude slower than your if( invariant ); therefore, it makes absolutely no sense to be worrying the slightest bit about the overhead of re-executing it in the loop.

More generally, an if( invariant ) is so trivial as to never be worth considering for optimization, even if it only controls trivial code.

Even more generally, things like if( invariant ) will be optimized by the C compiler in any way it sees fit, regardless of what your intentions are, so they are generally not worth considering.

And even more-more generally, one of the most important qualities of code is readability, (second only to correctness,) and more code is less readable than less code, so any approach that results in less code is preferable. In other words, any approach that requires more code is generally far worse than any approach that requires less code. Exceptions to this rule tend to be algorithmic optimizations, where you introduce an entire algorithm to achieve performance. (E.g. a hash map instead of a naive array scan.) Tweaking and hacking code all over the place to squeeze clock cycles here and there never results in anything good.

  • Related