what is the difference between statements below?

X = Y*0;
   X = 0;

a kind of optimization problem.

Recommended Answers

All 5 Replies

I think you can see the difference right there for yourself. This has nothing to do with optimization, unless you enable the -fretarded flag on your compiler.

I've never really heard of optimization on a high level programming language. In assembly perhaps.
So I would expect that X = 0 is more efficent that X = Y * 0;
The issue lies with memory access and compution. X = 0 accesses memory once where as the X = Y * 0 access memory twice and performs a calculation. If you look at the assembly commands to carry out these operations, you'll see the actual costs involved.

You don't optimize wrong code.. You correct it..
Why would anyone do x= y * 0; if they want to set x to 0 ?!

yeah as anything timesed by 0 = 0

i suppose x = 0 would be faster than x = y * 0 as its less steps?

You might have x = y * CONSTANT where #define CONSTANT 0 is located someplace. That's how you could get that in your code.

Whether x = y * 0 is compiled to anything different than x = 0 is compiled to depends on the compiler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.