4 ms·
Yes, but this is by far the least weird type of optimization, and the one you'd least want to turn off. It's the sort taught in undergraduate compiler design cl
by geofft 5y ago
Yes, but this is by far the least weird type of optimization, and the one you'd least want to turn off. It's the sort taught in undergraduate compiler design classes.
Here's a piece of code that would go slower if you turned it off:
for (i = 0; i < 1000; i++) {
struct foobar *ptr = malloc(sizeof(struct foobar));
ptr->this = a[i];
ptr->that = b[i];
if (ptr->this > 0) {
do_stuff(ptr);
}
free(ptr);
}
You only need to write to ptr->that if the condition succeeds. (And frankly you only need to do the allocation at all if a[i] > 0.)
Compilers that don't do this get rejected for compilers that do.