C/C++ Compiler Optimisation, multiplying by zero.

Started by
6 comments, last by All8Up 12 years, 2 months ago

Hi guys, just out of interest, I'm implementing an algoithm in which I'm using a mask on pixels (I'm doing a canny edge filter).

The mask has a column where the contents are multiplied by 0. I could just skip the column, by deleting the code for this column, but I want it to be clear.

If I times by a constant of zero will the compiler ignore that part of the code? Or will it perform the calculation?

Cheers Guys

James

Advertisement
[font=courier new,courier,monospace]float[/font] or [font=courier new,courier,monospace]int[/font]?

A float multiplied by zero doesn't always result in zero (e.g. +infinity * 0.0f == NaN) so it might not always be valid for the compiler to make that optimisation.
Also, if it's a very tight inner loop, where you add a zero check like an 'if (fabs(multiplier) < 1e-5f)' or 'if (multiplier == 0)', it can be much slower than just doing the multiplication, since branches aren't actually cheap either.

As always, profiling will tell.
Yeah I was thinking about an integer... So will it optimize for an integer? I can understand the float not.

James
Look at the generated assembly and see.

This isn't an FB topic, moving.
Profile these two cases:N = something big, megabytes.
int a[N];

for (int i = 0; i < N; i++) {
a = 0;
}

// vs.

for (int i = 0; i < N; i += 16) { // note the step
a = 0;
}
It should be obvious that second loop does 16 times less work, so it should run 16 times faster.

Here's results of a quick benchmark:(4299788 - 5144680) |#################################################################
(5144680 - 5989572) |############
(5989572 - 6834465) |##
(6834465 - 7679357) |##
(7679357 - 8524249) |##################
(8524249 - 9369142) |
(9369142 - 10214034) |
(10214034 - 11058927) |
5448480.2 [4299788.0 - 11058927.0]
0
(2871890 - 3112349) |###############################
(3112349 - 3352809) |#####################################
(3352809 - 3593268) |#####################
(3593268 - 3833728) |####
(3833728 - 4074188) |##
(4074188 - 4314647) |###
(4314647 - 4555107) |
(4555107 - 4795567) |#
3302247.6 [2871890.0 - 4795567.0]


First loop takes about : 5.5 million cycles
Second loop takes about 3.3 million cycles.

Or, despite writing 16 times less data, second loop is not even twice as fast.

Why?

Cache lines. Whether touching one or 64 bytes, simple operations complete faster than data can be fetched from memory.

The mask has a column where the contents are multiplied by 0. I could just skip the column, by deleting the code for this column, but I want it to be clear.

How about writing a comment instead?
I've never done a Canny filter myself so did a quick wikipedia to get a general idea. If I read it correctly and we are talking about a 5x5 matrix convolution then it is highly unlikely that a compiler will notice the zero's in a certain row/column given the non-algorithmic nature of the source data. I.e. reading from an array doesn't tell the compiler to look for a "pattern of zero's" which could be optimized out.

So, if I'm thinking in the correct direction for what you are actually doing, yeah, make sure you don't call that code. There are so many ways you could code this though, it's really up to the specifics and what you think should be optimized.

Of course, going to SIMD could be very useful, if it is 5x5 and one column or row is all zeros, perfect for a SIMD optimization.

This topic is closed to new replies.

Advertisement