It's a bit of a truism that all programming language tutorials must start with "Hello World" - the simplest application, which merely displays the message Hello World to the screen. Indeed, there's a few guides online to writing "Hello World" in every language imaginable.
As I've been playing with different languages as I learn to use my new Raspberry Pi, I thought make some notes on the versions I've been trying. As it's 2020, I think Hello, cruel world is a more appropriate message.
I'll start with C, since it's a pretty universal language across all computing platforms, it's fast, efficient, and needs very little support.
C
#include <stdio.h>
int main(void)
{
printf("Hello, cruel world\n");
}
ARM Assembly
.global _start
_start:
MOV r0, #1
LDR r1, =hello
LDR r2, =helloLen
MOV R7, #4
SVC 0
exit:
MOV R7, #1
SVC 0
hello:
.asciz "Hello, cruel world\n"
helloLen = .-hello
Java 11
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, cruel world");
}
}
Kotlin
package org.liamjd
class HelloCruel {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println("Hello, cruel world")
}
}
}
Python
print("Hello, cruel world")
Kotlin Native
fun main() {
println("Hello, cruel world")
}
Kotlin Native takes a Kotlin source file, and compiles it to a native binary executable, not running on the JVM. I've compiled it to Native ARM code for my Raspberry Pi, as well as a Windows X86 executable.
I thought it would be interesting to compare the sizes of the source code and the executable binaries produced. It's not a very fair comparison - the Kotlin and Java versions only execute in a Java Virtual Machine, which is about half a gigabyte of code. The C version relies on its own standard library, and even the ARM Assembly code needs a Linux execution environment. But is striking to see how large the Kotlin version is, with the need to include the Kotlin standard library in the JAR file. There are ways to reduce this, and ways of writing less Kotlin code, but the point stands. I've also included Python, as it is a key Pi programming language, but it's an interpreted language and so not in the same league as the others.
Language | Source Size (bytes) | Executable Size (bytes) |
---|---|---|
ARM Assembly | 176 | 832 |
C | 83 | 7984 |
Java 11 | 129 | 958 (JAR) |
Kotlin 1.4 | 191 | 1,694,836 ('Fat' JAR) |
Kotlin Native ARM | 46 | 328,604 |
Kotlin Native X86 | 46 | 512,199 |
Python 3 | 29 | n/a |
There's little value in comparing execution times on so simple a program, but let's try anyway!
Language | Time (seconds) |
---|---|
ARM Assembly | 0.004 |
C | 0.007 |
Java 11 | 0.336 |
Kotlin 1.4 | 0.352 |
Kotlin Native ARM | 0.01 |
Kotlin Native X86 | 0.023 |
Python 3 | 0.074 |
Why consider Kotlin Native? In my last post, I outlined some of the difficulties I've been having in writing Java code for the Raspberry Pi - the support libraries are not in place, really. I am a little concerned about the executable size from a Kotlin application and the need to have a full JVM running on the Pi. The Raspberry Pi 4 has all this, of course, and 4Gb of RAM is plenty. But the Pi Zero, a minimal Pi model, may struggle. Perhaps with Kotlin Native I could write Kotlin code and interface directly with the C libraries which support the Pi GPIO. I've found one YouTube video of a developer doing just this, but it's a lot of work and Kotlin/Native is a bit of a movable feast (and she had the advantage of developing on a Mac, which is much closer to Linux than my Windows computer).
More on my experiments with Kotlin Native in a later blog post.