lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
43 stars 52 forks source link

Performance advantage of using Codal from C++ nowhere mentioned - there should be examples #337

Open lvml opened 1 year ago

lvml commented 1 year ago

Looking at the web-pages of both microbit.org and on the Codal documentation/samples for the Micro:Bit v2 boards, I noticed that software performance was not advertised as a great benefit over using script-languages like MicroPython on these tiny boards.

I remember, when I was a child, a few lines of BASIC compared with a few lines of 6502-Assembler, run on a Commodore C64 to just cycle through screen colors, was all it took to immediately convince me that learning "machine language" was worth the effort.

It would really be a missed opportunity to not mention the speed differences between different programming languages, as it is easy to provide an impressive little example: Here is a little program that just increments a counter variable, and every 4096 counts, the more significant bits of the counter are displayed as one bit per pixel. The user can also press the A-button to pause the increments or press B to continue. Find below a version in MicroPython and one in C++ - the difference is easily visible with the naked eye, the C++ version runs about 100 to 200 times faster than the MicroPython version! Children should know about this.

Feel free to use these code snippets as examples in your microbit-v2-samples repository, if you like. There may be more sophisticated ideas to demonstrate "what better performance can do for you", but I wanted to keep the code examples so simple that the similarity of the code is easy to spot.

MicroPython version:

from microbit import *
imgbuf = bytearray(bytes(25))

c = 0
while True:
    c += 1
    if c & 0xfff != 0:
        continue

    if button_a.is_pressed():
        while True:
            if button_b.is_pressed():
                break
            sleep(100)

    i = c >> 12
    for p in range(25):
        imgbuf[p] = -(i & 1)
        i >>= 1
    display.show(Image(5, 5, imgbuf), delay=0, wait=False)

C++ version:

#include "MicroBit.h"
#include "Image.h"

MicroBit uBit;

int main() {
    uBit.init();
    uBit.display.setDisplayMode(DISPLAY_MODE_BLACK_AND_WHITE);

    uint32_t c = 0;
    while(true) {
        c += 1;
        if ((c & 0xfff) != 0) {
            continue;
        }

        if (uBit.buttonA.isPressed()) {
            while(true) {
                if (uBit.buttonB.isPressed()) {
                    break;
                }
                uBit.sleep(100);
            }
        }

        unsigned int i = c >> 12;
        uint8_t * img_buf_p = uBit.display.image.getBitmap();
        uint8_t * img_buf_end = img_buf_p + 25;
        while (img_buf_p < img_buf_end) {
            *img_buf_p++ = -(i & 1);
            i >>= 1;
        }
    }
}
JohnVidler commented 1 year ago

This is a good point, actually - I too remember looking at similar books for BASIC and being amazed at the speed difference when dropping to a lower level language.

If you're able, would you be up for writing an article using these examples over at https://github.com/lancaster-university/codal-documentation ? I'd suggest as a project for now, so under this path: https://github.com/lancaster-university/codal-documentation/tree/master/docs/projects

Happy to accept any PRs for this kind of example/article over there :)

lvml commented 1 year ago

If you're able, would you be up for writing an article using these examples over at https://github.com/lancaster-university/codal-documentation ? I'd suggest as a project for now, so under this path: https://github.com/lancaster-university/codal-documentation/tree/master/docs/projects

Happy to accept any PRs for this kind of example/article over there :)

Ok, find my PR here: https://github.com/lancaster-university/codal-documentation/pull/5