multiple return values 
Author Message
 multiple return values

Quote:

> Is there a penalty for binding new variables with the same
> name at every iteration of a looping construct?

In typical implementations, I expect not.

Quote:
> For example:
>   for (m in months)
>     let work-days = ... ;
>   end for;
> vs:
>   let work-days = 0;
>   for (m in months)
>     work-days := ... ;
>   end for;
> A C programmer would avoid the former, thinking that a new local
> variable would be created (possibly from the stack) at every
> iteration.

I hope a typical C programmer wouldn't think that.  For most C
compilers, thinking such a thing would be incorrect.

Quote:
> I suspect that that is not the case in Dylan, but never read it
> explicitly anywhere... and I know very little of Lisp.

I don't think you can say always, but I'd be very surprised if any Dylan
implementation makes the second version less expensive than the first,
given the difficulties of optimizing assignment.  Good compilers will
notice that the initialization with 0 isn't used and omit that piece of
code, and then generate identical instructions for those loops.

Paul



Fri, 17 Apr 1998 03:00:00 GMT  
 multiple return values
Is there a penalty for binding new variables with the same
name at every iteration of a looping construct?
For example:

 for (m in months)
   let work-days = ... ;
 end for;

vs:

 let work-days = 0;
 for (m in months)
   work-days := ... ;
 end for;

A C programmer would avoid the former, thinking that a new local
variable would be created (possibly from the stack) at every
iteration.
I suspect that that is not the case in Dylan, but never read it
explicitly anywhere... and I know very little of Lisp.

That would also make the problem of multiple assignment a non-problem
(almost).

  Enrico



Fri, 17 Apr 1998 03:00:00 GMT  
 multiple return values

Quote:

> Is there a penalty for binding new variables with the same
> name at every iteration of a looping construct?
> For example:

>  for (m in months)
>    let work-days = ... ;
>  end for;

> vs:

>  let work-days = 0;
>  for (m in months)
>    work-days := ... ;
>  end for;

> A C programmer would avoid the former, thinking that a new local
> variable would be created (possibly from the stack) at every
> iteration.
> I suspect that that is not the case in Dylan, but never read it
> explicitly anywhere... and I know very little of Lisp.

> That would also make the problem of multiple assignment a non-problem
> (almost).

In any reasonable C compiler the local version is no worse than the
global version, and it is usually better because it gives the compiler
an easier time of deciding whether the register holding the value can
be reused, or whether it needs to be retained for the next iteration.

I've never seen a compiler that spends time allocating and deallocating
stack space for such nested variables every time around a loop -- it is
very easy and normal to determine the maximum stack requirement for the
entire function, and allocate particular stack slots statically (or
registers used in a stack discipline in a compiler that has lots of
registers but doesn't do colouring and value lifetime analysis)

Always use the "let" form if you can.

-- Bruce



Sat, 18 Apr 1998 03:00:00 GMT  
 multiple return values

Quote:

>Subject: re: multiple return values

>Is there a penalty for binding new variables with the same
>name at every iteration of a looping construct?

This depends on the implementation, but in general the answer
is no.

Quote:
>For example:

> for (m in months)
>   let work-days = ... ;
> end for;

>vs:

> let work-days = 0;
> for (m in months)
>   work-days := ... ;
> end for;

In the Apple Dylan Technology Release, the former will compile much
more efficiently.  (There's no good reason, beyond lack of time to
put in a particular optimization.)

----------------------------------------------------------------------------
Andrew Shalit                            84% of all factoids are innacurate.

or, until 11/3:                          .



Sun, 19 Apr 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ???Eiffel idiom for multiple return values???

2. Tuples, iterators, and multiple return values in Eiffel?

3. Multiple return values

4. Multiple return values

5. Newbie on multiple return values

6. multiple return values for functions

7. Multiple return values

8. 1st-class method closures (was Re: Multiple return values)

9. C-interface and multiple return values

10. multiple Return values

11. Destructuring / pattern-matching (was: Multiple return values)

12. Destructuring / pattern-matching (was: Multiple return values)

 

 
Powered by phpBB® Forum Software