Spin vs. C Speed
Sal Ammoniac
Posts: 213
in Propeller 1
Is code written in C any faster than code written in Spin on the Propeller?
I've written an SPI master driver in Spin and the maximum transfer rate I can get is 14K bps. If I recode this in C can I get faster rates?
(I know I can get faster rates in PASM, but all I'm looking for here is a quick and dirty SPI routine to test some slave code that I've written for another platform.)
I've written an SPI master driver in Spin and the maximum transfer rate I can get is 14K bps. If I recode this in C can I get faster rates?
(I know I can get faster rates in PASM, but all I'm looking for here is a quick and dirty SPI routine to test some slave code that I've written for another platform.)
Comments
C should be faster.
C using the LMM memory model will also be faster than Spin but you won't be able to fit as much code in 32k of hub memory since LMM code is less dense than Spin bytecodes.
C using the CMM memory model will be about the same speed as Spin and use about the same amount of memory.
To setup SPI pins with SCK on 0, MOSI on 1, MISO, on 2, SS on 3 just type:
&03.02.01.00 SPIPINS
To write 8 bits at a time, say $1A type:
$1A SPIWRB
and to release SS which is automatically enabled on any transfer type SPICE
A snippet of code that writes an SPI long to the WIZnet W5500 chip:
I like kwinn's answer, short and sweet. All of the different models available via PropGCC are faster than Spin.
However, since you're just looking to test some slave code on another chip, you might be better off switching libraries within your current language rather than switching languages (and therefore toolchains). This post mentions that the Propeller Tool ships with an assembly version of the SPI library, meaning you wouldn't have to code it up, so it should be very easy to test with. Look for SPI_ASM.spin.
And finally, if you're going to jump to PropGCC, I PropWare's SPI library will run 4 MHz in CMM mode. Example of writing to another microcontroller and expecting an echo response would look like this.
C: Faster but too big.
C with CMM: Perhaps faster and smaller. Who knows actually.
Forth: SPIWR SPIWR SPIWR SPIWR DROP ......
So PASM it is.
However, PASM is also too big. What then? Well, I guess the answer has to be a combination of more than one of the above. Spin+PASM has certainly been used effectively in many projects and CMM+PASM could probably be used effectively as well. I believe it is possible to combine assembly with Tachyon as well although it is probably not necessary there as often since Tachyon itself is quite fast compared with Spin or C unless you use the COG memory model with C. Anyway, I realize that you weren't completely serious in your post. I agree that there is no clear choice if you want to use a single language in your program.
Edit: Never mind. I found them on a second attempt. I have no idea why my first search failed. Sorry for the false alarm!
Yes, once indoctrinated, most people find it difficult to accept new ideas or different ways of doing things. Imagine if people were indoctrinated in "Forth" first!!
My experience is that CMM code is about twice as fast as Spin but about 10-20% larger. This is a generalization, of course, individual programs will differ, but I've run a lot of benchmarks and the result holds in most cases (for CMM programs compiled with -Os, that is). CMM also offers you the choice of compiling with FCACHE mode (-O2) and depending on the program that can provide a huge speedup.
The problem could have already been solved and the OP could have moved on.
So heater is right, just a few { ** * -> & } and the problem will magically be solved, sheesh.
Yes, that's my experience as well. I said "about the same speed" since I remember someone posting code that was actually faster in Spin. Of course, I can't recall the thread so maybe I should just keep my mouth shut! :-)
As for Forth--I'm not a Kool-Aid drinker (as someone else on this thread so eloquently put it.) I've tried to like Forth over the years but have never been able to get my head around it. One of my best friends calls it "that hippy language".
If your code is already written in Spin, why not give fastspin a try? fastspin will take your existing code and compile it to LMM that's way faster than the Spin interpreter.
If you're already a C programmer and prefer to use that, you can use spin2cpp (which is very closely related to fastspin) to convert your Spin code to C as a starting point.
Original SPI code:
Compiled PASM equivalent:
CMM: 1.33 Mbps
LMM: 1.33 Mbps
COG: 1.43 Mbps
I'm surprised to see no difference between the CMM and LMM results and am also surprised to see the COG model results so slow.
These results are much better than the 14 Kbps I was getting with Spin, but still disappointing.