Subject:
|
Re: NQC, what gives the fastest code ?
|
Newsgroups:
|
lugnet.robotics.rcx.nqc
|
Date:
|
Wed, 9 May 2001 03:09:55 GMT
|
Viewed:
|
1991 times
|
| |
| |
It has very little to do with C itself - its really a question of
compiler optimization. NQC doesn't have any explicit optimization
passes, and there is no data flow or variable lifetime analysis, so
often the assembly will be a bit inefficient.
However, I have optimized a few cases - specifically if the result of a
math operation is going back into the variable holding one of the
operands, then it gets optimized. For example with:
x = x / 128;
the compiler knows it can trash the old value of x during the
computation provided it gets the right answer, so does the math in-place
on x rather than compute in a temporary and move the temp to x.
In your second case:
SetSerialData(2,(x / 128));
The second argument (x/128) has to be computed in a temporary location
since the compiler doesn't check to see if x is used later in the
program, so will try to keep it intact. Therefore, instead of operating
on x in place, it moves x to a temp, divides the temp, and uses the temp
as the argument.
My advice would be to write the code the way you want and only tweak if
it becomes necessary (e.g. program is operating too slowly). Then only
bother to tune what needs to be tuned. If you want the most control
during this "tuning", then you'll want to break the math up into
individual math operations and always use assignment math...
x /= 128;
For the operations that the RCX supports (+, -, *, /, &, |) these
assignments will always be a single instruction when a constant or
variable is used as the operand. An extra instruction is required for
some other types of operands due to RCX 1.0 limitations. RCX 2.0 is a
bit more flexable, and soon-to-be-released NQC 2.4 will take this into
account when generating code for RCX 2.0.
Dave Baum
In article <3AF8645E.6CA3D578@ids.kun.nl>, Stef Mientki
<s.mientki@ids.kun.nl> wrote:
> hi All,
>
> I'm not a C-programmer, so maybe I'm asking a (very) stupid question.
> In searching for the fastest algoritme, I looked into the code generated
> by NQC (through RCXcc-orginal ;-)).
> I noticed that when you write your code in basic elementary 2 operand
> instructions, it becomes a lot faster.
> In the example below, I would like (as a Pascal programmer) to write the
> code surrounded by "AAAA",
> but the code surrounded with "BBBB" is much faster.
> The differences become larger as the calculations become more complex.
>
> Am I using the C-language wrong ?
> Is this general behaviour of C ?
> Is this the behaviour of the used compiler ?
> or what else ??
>
>
> THE EXAMPLE PROGRAM
> task testnqc()
> {
> int x;
>
> // AAAA the code I prefer to write
> x=SENSOR_1;
> SetSerialData(1,x);
> SetSerialData(2,(x / 128));
> // AAAA
>
>
> // BBBB but this code is more efficient
> x=SENSOR_1;
> SetSerialData(1,x);
> x=x/128;
> SetSerialData(2,x);
> // BBBB
> }
>
>
> CODE GENERATED BY NQC
> *** Task 1 = testnqc
> 000 setv var[47], Input(0) 14 2f 09 00 00
> 005 set SerSetting(1), var[47] 05 21 01 00 2f 00
> 011 setv var[46], var[47] 14 2e 00 2f 00
> 016 divv var[46], 128 44 2e 02 80 00
>
> 021 set SerSetting(2), var[46] 05 21 02 00 2e 00
> 027 setv var[47], Input(0) 14 2f 09 00 00
> 032 set SerSetting(1), var[47] 05 21 01 00 2f 00
> 038 divv var[47], 128 44 2f 02 80 00
> 043 set SerSetting(2), var[47] 05 21 02 00 2f 00
>
>
>
> Stef Mientki
>
>
--
reply to: dbaum at enteract dot com
|
|
Message has 1 Reply: | | Re: NQC, what gives the fastest code ?
|
| Thanks Dave !! That was a very good explanation! In return a problem (and solution) with the serial data transmission (not an NQC error, but you could add it to your documentation). In doing fast data transfer from RCX to PC, there seems to be a (...) (23 years ago, 9-May-01, to lugnet.robotics.rcx.nqc)
|
Message is in Reply To:
| | NQC, what gives the fastest code ?
|
| hi All, I'm not a C-programmer, so maybe I'm asking a (very) stupid question. In searching for the fastest algoritme, I looked into the code generated by NQC (through RCXcc-orginal ;-)). I noticed that when you write your code in basic elementary 2 (...) (23 years ago, 8-May-01, to lugnet.robotics.rcx.nqc)
|
3 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|