Which is faster?

if p1_y > 125
	Pwm1_green = 1;
	Pwm1_green = 0;


Pwm1_green = p1_y / 126;

I would say #2, but i’m not an expert on optimization.

I think it depends on the processor. Fancy pipelined processors don’t do well with branches and if statements. I’m pretty sure the PIC isn’t that fancy. The only real question is if it can complete a divide in one clock cycle or not. I’d bet on the latter being faster, though. The branch instruction is more likely to delay the processor more than a simple integer divide. Either one is going to be pretty darned fast, though.

Well it will be different on every CPU, but to give you an idea, on x86 machines, it compiles down to 27 and 31 lines of ASM for #1 and #2 respectively. So basically, the first has few instructions at a lower level. Though that still may not give much indication as to how fast it will run on the IFI PIC’s. Especially with all the variables and aliases involved in the operation.

Nate, x86 has a built in divide instruction, while the PIC does not, so I’d expect both to be similar in number of instructions on x86 but not on the PIC. You didn’t say how long each took though, because the divide instruction will take several cycles to execute.

Gary, You can put both in your code and compare them in the list file. That will give you the exact answer for the PIC.

Also, is it absolutely necessary to divide by 126? It would be much faster to divide by 128, which boils down to a rotate and an AND.