Sign in to follow this  
JamesCobras

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

Recommended Posts

JamesCobras    112

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

Share this post


Link to post
Share on other sites
Hodgman    51234
[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 ([i]e.g. +infinity * 0.0f == NaN[/i]) so it might not always be valid for the compiler to make that optimisation.

Share this post


Link to post
Share on other sites
clb    2147
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.

Share this post


Link to post
Share on other sites
Antheus    2409
Profile these two cases:[code]N = something big, megabytes.
int a[N];

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

// vs.

for (int i = 0; i < N; i += 16) { // note the step
a[i] = 0;
}[/code]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:[code](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][/code]

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.

Share this post


Link to post
Share on other sites
alvaro    21246
[quote name='JamesCobras' timestamp='1327410223' post='4905771']
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.[/quote]
How about writing a comment instead?

Share this post


Link to post
Share on other sites
Hiwas    5807
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this