Subject:
|
Re: floating point operation
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Wed, 28 Nov 2001 20:28:43 GMT
|
Viewed:
|
1724 times
|
| |
| |
On Wed, 28 Nov 2001, Dennis Diehl wrote:
> I did some experiments with floating point operations and I was surprised
> when I tried this:
>
>
> int main(){
> long t1;
> int i,j;
> float t;
> t1 = sys_time;
> for(j=0; j<1000; j++){
> t=0.06;
> for(i=0; i<100; i++){
> t=1.7*t;
> }
> }
> lcd_int((sys_time - t1)/1000);
> return 0;
> }
>
> this program terminated after 1 second, but the following program
> took about 52 seconds:
>
> int main(){
> long t1;
> int i,j;
> float t;
> t1 = sys_time;
> for(j=0; j<1000; j++){
> t=0.06;
> for(i=0; i<100; i++){
> if (t>0) t=1.7*t;
> /* -------- */
> }
> }
> lcd_int((sys_time - t1)/1000);
> return 0;
> }
>
> What is the reason for this difference?
I suspect that when you look at the assembler code produced by the
compiler you will find in the first instance it was able to optimize the
FP multiply out of the inner loop since it didn't depend on the loop
induction variable. Ie, it simply did t=1700*t. I would also not be
surprised if the entire outer loop wasn't optimized away as well. Adding
the if made it to complicated for the compiler to optimize away.
To keep the compiler from optimizing away things you want to keep, either
put it in a separate function compiled separately (and do the comparison
to an empty function so you know how much is spent in the thing you want
to test) or use the loop induction variables in the computation (but
beware that the software FP code probably has data-dependent performance,
so you may get different performance numbers depending on what values you
use for the loop).
John A. Tamplin jat@jaet.org
770/436-5387 HOME 4116 Manson Ave
770/431-9459 FAX Smyrna, GA 30082-3723
|
|
Message is in Reply To:
| | floating point operation
|
| I did some experiments with floating point operations and I was surprised when I tried this: int main(){ long t1; int i,j; float t; t1 = sys_time; for(j=0; j<1000; j++){ t=0.06; for(i=0; i<100; i++){ t=1.7*t; } } lcd_int((sys_time - t1)/1000); (...) (23 years ago, 28-Nov-01, to lugnet.robotics.rcx.legos)
|
4 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|