The difference between loops and recursion in Java, C and C++ on a Raspberry Pi

To get the ball rolling with my programming series on the Raspberry Pi, I decided to create a screen cast on ‘recursion verses loops’. ┬áIn addition I thought that it would be a good idea to compare the different execution time when implemented on Java and C.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

I then thought it would be a good idea to create a C++ version:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

Java code:

// Recursion vs loops example.
// (c) G J Barnard May 2013
// License:
// http://creativecommons.org/licenses/by-nc-nd/3.0/

public class Recursion {
    private byte aNumber = 42;

    public static void main(String args[]) {
        Recursion us = new Recursion();
        long currentTime;

        System.out.println("Loop method:");
        currentTime = System.nanoTime();
        us.ALoop((byte)0);
        System.out.println("Took: " + ((System.nanoTime() - currentTime) / 1000) + " micro seconds.");

        System.out.println("Recursion method:");
        currentTime = System.nanoTime();
        us.ARecursion((byte)0);
        System.out.println("Took: " + ((System.nanoTime() - currentTime) / 1000) + " micro seconds.");
    }

    public void ALoop(byte number) {
        while (number <= aNumber) {
            System.out.print(".");
            number++;
        }
        System.out.println("done");
    }

    public void ARecursion(byte number) {
        System.out.print(".");
        number++;
        if (number <= aNumber) {
            ARecursion(number);
        } else {
            System.out.println("done");
        }
    }
}

C code:

/* Recursion vs loops example.
(c) G J Barnard May 2013
License:
http://creativecommons.org/licenses/by-nc-nd/3.0/
*/
#include <stdio.h>
#include <sys/time.h>

unsigned char aNumber = 42;

void ALoop(unsigned char number) {
    while (number <= aNumber) {
        printf(".");
        number++;
    }
    printf("done\n");
}

void ARecursion(unsigned char number) {
    printf(".");
    number++;
    if (number <= aNumber) {
        ARecursion(number);
    } else {
        printf("done\n");
    }
}

int main() {
    struct timeval start, end;
    unsigned long duration = 0;

    printf("Loop method:\n");
    gettimeofday(&start, NULL);
    ALoop(0);
    gettimeofday(&end, NULL);
    duration = ((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec - start.tv_usec;
    printf("Took: %lu micro seconds.\n", duration);

    printf("Recursion method:\n");
    gettimeofday(&start, NULL);
    ARecursion(0);
    gettimeofday(&end, NULL);
    duration = ((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec - start.tv_usec;
    printf("Took: %lu micro seconds.\n", duration);
}

C++ code:

/* Recursion vs loops example.
(c) G J Barnard May 2013
License:
http://creativecommons.org/licenses/by-nc-nd/3.0/
*/
#include <iostream>
using namespace std;
#include <sys/time.h>

class Recursion {
    private:
        unsigned char aNumber;

    public:
        Recursion() : aNumber(42) {
    }

    /* The loop method */
    void ALoop(unsigned char number) {
        while (number <= aNumber) {
            cout << ".";
            number++;
        }
        cout << "done" << endl;
    }

    /* The recursive method */
    void ARecursion(unsigned char number) {
        cout << ".";
        number++;
        if (number <= aNumber) {
            ARecursion(number);
        } else {
            cout << "done" << endl;
        }
    }
};

int main() {
    struct timeval start, end;
    unsigned long duration = 0;
    Recursion us;

    cout << "Loop method:" << endl;
    gettimeofday(&start, NULL);
    us.ALoop(0);
    gettimeofday(&end, NULL);
    duration = ((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec - start.tv_usec;
    cout << "Took: " << duration << " micro seconds." << endl;

    cout << "Recursion method:" << endl;
    gettimeofday(&start, NULL);
    us.ARecursion(0);
    gettimeofday(&end, NULL);
    duration = ((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec - start.tv_usec;
    cout << "Took: " << duration << " micro seconds." << endl;

    return 0;
}