Code size: Spin Vs. Assembly ?
Rayman
Posts: 14,793
Ok, so assembly is 80 to 200 times faster than Spin (so says deSilva).· But what about the relative size of the code to do the same things?
It appears to me that SPIN compiles to very small code, perhaps comparable in size to the equivalent assembly code.· But, I haven't looked at this in any detail.
Anybody looked at this?
It appears to me that SPIN compiles to very small code, perhaps comparable in size to the equivalent assembly code.· But, I haven't looked at this in any detail.
Anybody looked at this?
Comments
This is one reason why interpreted langauages can be argued to be better than compiled. It takes very little room to store the tokenized version of the program.
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
SelmaWare Solutions - StampPlot GUI for controllers, XBee and Propeller Application Boards
Southern Illinois University Carbondale, Electronic Systems Technologies
I wouldn't really call the Spin bytecode "tokens" either, not in the same way that the Basic Stamp and other home computers of the 80's tokenised programs.
Anyway, back to the issue; Spin size versus Assembly size ... I don't think there's any easy comparison to be had in general.
Spin bytecode is quite efficiently optimised so common things like pushing zero or one to the stack is a single byte, and popping it to some global or local (stack) variable is also a byte, so that's a 2:1 improvement, more so if moving a number over 511 which requires two longs in Assembler but could be as short as three bytes in Spin. Then again it depends on the value of the number and where's it going; the advantage moves between the two.
There are some specialised opcodes which make Case and Repeat very space efficient, but then one would probably use different techniques in Assembler than in Spin. Spin isn't optimised, so an unknown 'poor choice' of coding style can have an impact on both space and execution time.
Trying to compare at the opcode level is near impossible ( they are very different architectures ), only in terms of particular tasks could a more realistic comparison be made IMO.
Spin appears to be more compact than Assembler but I'm not sure if that's simply because it has 32KB instead of 2KB to reside in. My gut feeling is that they both achieve the same code density overall.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
Things like var++, var~ look to be more efficient and faster than var+=1, var:=0 because the later push then pop while the former work with reference to a pointer so there should be a lot less interaction with hub memory going on. Accessing byteArray[noparse][[/noparse] i ] may be faster than using 'pointers' in Spin ( byte[noparse][[/noparse] @byteArray + i ] ) because there are specific opcodes to do indexing from an offset.
The compiler really is non-optimising so one instant saving is to get used to using 'constant'.
var := var + "A" - "0"
will do three pushes, an add, a subtract and a pop.
var += constant( "A" - "0" )
is a lot more efficient.
Likewise if using CON values to control flow ...
if PIN_NUMBER => 28
if constant( PIN_NUMBER => 28 )
----
The space efficiency is a matter of what has been "anticipated" in the design of the SPIN interpreter.
Recursive calls of procedures (or just decent calls of precedured in the first place) have.
Procedure variables (pointers to methods) have not...
There was a nice example the other day: "How to compute the parity of a word?". This is just ONE machine instruction. The SPIN code is awkward...
In this FIBONACCI example we need:
36 bytes for the SPIN byte code
152 bytes for machine code
This is most likely an extreme situation; however note that you have to augment your assembly program by many goodies (multiplication, division, searching,..) freely available in the SPIN COG... Such things add to size...
A most important consideration is how to work with algorithmically really complex programs... Their max size can be 8 k machine instructions, using a clever swapping technique; realistically it will not be more than 4k instructions.....
This will be also the bottleneck for future C-programs, until they provide a SD-card overlay swapping....