[slang-users] advantage of $1 over a variable

John E. Davis davis at space.mit.edu
Thu Jun 23 12:20:10 EDT 2005


Joerg Sommer <joerg at alea.gnuu.de> wrote:
>I ask myself often, what is the advantage of $1 or $2 (and so on) over a
>real variable with a name? Is there any?

They are pre-declared "convenience" variables.  With that said _never_
use them in functions.  slang has a number of optimizations that
involve local variables and relatively few involving global variables.
Here is a simple slsh experiment that illustrates this:

   slsh> define foo1 () { loop (10000000) $1 = $1-2*$1+3*$1-4*$1; }
   slsh> define foo (x) { loop (10000000) x = x-2*x+3*x-4*x; }     
   slsh> tic; foo(5);toc;                                          
   7.12571
   slsh> tic; $1=5; foo1();toc;                                    
   10.2782

This indicates that the version involving the local variable is about
30% faster.

--John




More information about the slang-users-l mailing list