Post Reply 
RPL programming questions
02-19-2014, 01:49 PM
Post: #9
RE: RPL programming questions
(02-19-2014 09:52 AM)HP67 Wrote:  Curiously, David's example of locals is faster than Peacecalc's example using the stack only. This does not seem to make sense.
The difference is sysRPL vs userRPL. In general, sysRPL commands don't check their arguments - they leave that to the programmer. userRPL commands always check and that overhead is significant.
(02-19-2014 09:52 AM)HP67 Wrote:  This is what I have been asking about. I suppose but am not certain that a local variable takes more space than a stack item if not at least for creating a name and some method of pointing to the value. That's the storage cost over a stack entry. And there is also the issue of how the variables are accessed by name.
An entry on the stack is just a pointer to an existing object. That takes 5 nibbles (2.5 bytes). Accessing the stack with words like SWAP, OVER and ROT takes 2.5 bytes each. A sequence like 4. PICK takes 5 bytes - 2.5 for "4." and 2.5 for "PICK". Note that "4" is only 2.5 bytes because the calc has copies of small integer REALs in ROM and the compiler is smart enough to use them when it sees a reference to the values.

The time required to access the stack is very fast - just a bit of binary arithmetic.

When you store a local variable, the calculator uses two pointers: one points to the local variable name (called a LAM) and the other points to the stored object. There are also 5 bytes of house keeping data for each block of locals. For example, when you execute "-> A B C << " the calculator needs 5 bytes of housekeeping plus 2 pointers for each of the 3 local variables for a total of 5+2.5*2*3 = 20 bytes.

If you refer to a local by name, the name takes 1 byte for each character plus 3.5 bytes of overhead. e.g. "A" takes 4.5 bytes and "AB" takes 5.5. Note that if you just put a local name in your program, the effect is ALWAYS to recall the variable whereas a global name will EXECUTE the variable. If the variable is something like a REAL, the results are the same, but if it's a program stored in a global, then you'll also need to EVAL the program if you want to run it. That takes another 2.5 bytes.

To lookup a local variable, the calculator scans through all the locals looking for one whose name matches. Thus shorter names take less time to scan. In sysRPL, you have the ability to say "get me the 3rd local variable" and the calc can find it with a little arithmetic - comparable to a PICK operation on the stack.

Local variables with short names are pretty fast and they make the code MUCH easier to write, read and maintain. I'd code your program using them first. If performance becomes an issue then you can think about modifying it to use the stack.

Dave
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RPL programming questions - HP67 - 02-17-2014, 01:54 PM
RE: RPL programming questions - peacecalc - 02-17-2014, 03:10 PM
RE: RPL programming questions - Joe Horn - 02-18-2014, 01:06 PM
RE: RPL programming questions - HP67 - 02-18-2014, 01:39 PM
RE: RPL programming questions - peacecalc - 02-18-2014, 07:15 PM
RE: RPL programming questions - DavidM - 02-18-2014, 11:31 PM
RE: RPL programming questions - HP67 - 02-19-2014, 09:52 AM
RE: RPL programming questions - David Hayden - 02-19-2014 01:49 PM
RE: RPL programming questions - HP67 - 02-19-2014, 02:15 PM
RE: RPL programming questions - HP67 - 02-20-2014, 03:27 PM
RE: RPL programming questions - DavidM - 02-19-2014, 05:23 PM
RE: RPL programming questions - HP67 - 02-20-2014, 03:40 PM
RE: RPL programming questions - DavidM - 02-20-2014, 04:10 PM
RE: RPL programming questions - peacecalc - 02-19-2014, 04:15 PM
RE: RPL programming questions - HP67 - 02-20-2014, 04:36 PM
RE: RPL programming questions - RMollov - 02-24-2014, 12:47 PM



User(s) browsing this thread: 1 Guest(s)