hippy
Posts: 6709
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Dart Native on Raspberry Pi

Fri Nov 08, 2019 6:39 pm

I happened to venture across this -

https://www.theregister.co.uk/2019/11/0 ... dart_build
Google software engineers have delivered Dart 2.6, an update to the open source programming language that provides the ability to create self-contained, native executables for the major desktop operating systems.

That capability comes from an extension to the Dart compiler set called dart2native, which makes it possible to turn Dart files into self-contained executables holding ahead-of-time compiled machine code. In other words, these executables will run on machines that don't have the Dart SDK installed.

Dart 2.6 also includes an improved version of dart:ffi, which provides C interoperability, and a new language feature called extension methods.
And support for that includes the Pi. For ARMv7 upwards it is an easy enough process with a downloadable Dart SDK for ARM provided. For ARMv6 ( Pi Zero, Zero W, A, B, A+ and B+ you will need to build the SDK yourself.

Code: Select all

cd ~
wget https://storage.googleapis.com/dart-archive/channels/stable/release/2.6.0/sdk/dartsdk-linux-arm-release.zip
unzip dartsdk-linux-arm-release.zip
./dart-sdk/bin/dart --version
Compilation isn't exactly fast but it does seem to work, generating a native ARM executable, albeit a quite large one and with a .exe extension by default.

fibo.dart

Code: Select all

int Fibonacci(int n) {
  if (n <= 1) { return n; }
  else        { return Fibonacci(n-1) + Fibonacci(n-2); };
}

main() {
  print(Fibonacci(24));
}
On my Pi 3B (non-plus) ...

Code: Select all

[email protected]:~ $ time ./dart-sdk/bin/dart fibo.dart
46368

real    0m4.323s
user    0m5.529s
sys     0m0.241s

Code: Select all

[email protected]:~ $ time ./dart-sdk/bin/dart2native fibo.dart
Generated: /home/pi/fibo.exe

real    0m35.183s
user    0m41.626s
sys     0m1.839s

Code: Select all

[email protected]:~ $ time ./fibo.exe
46368

real    0m0.059s
user    0m0.058s
sys     0m0.002s

Code: Select all

[email protected]:~ $ ls -l fibo.exe
-rwxr-xr-x 1 pi pi 6284048 Nov  8 18:24 fibo.exe

Code: Select all

[email protected]:~ $ file fibo.exe
fibo.exe: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, stripped

User avatar
tipam
Posts: 99
Joined: Fri Dec 30, 2011 1:32 pm
Contact: Website

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 12:51 am

I think its worth mentioning that dartpad.dev works well on RPi as a web app.

https://dartpad.dev

Check it out!
Raspberry Pi == small computer == big dreams

Heater
Posts: 14430
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 8:46 am

I make that about 10 times slower than C or Rust on a Pi 3B:

Code: Select all

fn fibonacci(n: i32) -> i32 {
    match n {
        0..=1 => n,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

fn main () {
    let n = 24;
    println!("fibo({}) = {}", n, fibonacci(n));
}

Code: Select all

$ rustc  -C opt-level=3   fibo.rs
$ time ./fibo
fibo(24) = 46368

real    0m0.005s
user    0m0.000s
sys     0m0.005s

Code: Select all

#include "stdio.h"

int fibonacci(int n) {
    if (n < 2) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main () {
    int n = 24;
    printf("fibo(%d) = %d\n", n, fibonacci(n));
}

Code: Select all

$ gcc -Wall -O3 -o fibo fibo.c -march=native -mtune=native
$ time ./fibo
fibo(24) = 46368

real    0m0.005s
user    0m0.001s
sys     0m0.004s
It would better to do the timing with a bigger n. So as to get a more stable result and separate out load time from run time. Say n = 42:

Code: Select all

$ time ./fibo
fibo(42) = 267914296

real    0m8.482s
user    0m8.479s
sys     0m0.001s
Memory in C++ is a leaky abstraction .

jahboater
Posts: 5027
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 10:42 am

Heater wrote:
Sat Dec 07, 2019 8:46 am
I make that about 10 times slower than C or Rust on a Pi 3B:
I bet the C version (and probably the rust version) are smaller than 6.2MB too!
Edit:
In fact on my PC, the C version takes:

Code: Select all

$ filefrag -v fibo
Filesystem type is: ef53
File size of fibo is 4312 (2 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..       1:    8459756..   8459757:      2:             last,eof
fibo: 1 extent found
That's 2 disk blocks instead of 1536 blocks - must be quicker to load.

hippy
Posts: 6709
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 11:45 am

Heater wrote:
Sat Dec 07, 2019 8:46 am
I make that about 10 times slower than C or Rust on a Pi 3B:
Faster than Python though. As always, horses for courses, and fastest execution isn't the most important requirement for everyone.

It's not necessary to derail every language thread with 'my preferred choice of language is better than yours'. I thought this thread had escaped that fate, but apparently not.

Heater
Posts: 14430
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 11:55 am

hippy,
...fastest execution isn't the most important requirement for everyone.
So why did you open with micro benchmark timing results then? And no other suggestion as to why Dart might be attractive.
It's not necessary to derail every language thread with 'my preferred choice of language is better than yours'.
Yes indeed. And that is why I don't do that. And certainly not in this case. I did not say anything about my personal choice. I did not say anything about "better".

What I did was comment that your benchmark seemed slow.
Offer two examples for comparison to indicate why I think it seemed slow.
Offer a suggestion as to how to actually make the benchmark measure the right thing.

I look forward to seeing the result of that suggestion.
I thought this thread had escaped that fate, but apparently not.
It had. Until you brought it up.
Memory in C++ is a leaky abstraction .

hippy
Posts: 6709
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 2:08 pm

Heater wrote:
Sat Dec 07, 2019 11:55 am
hippy,
...fastest execution isn't the most important requirement for everyone.
So why did you open with micro benchmark timing results then? And no other suggestion as to why Dart might be attractive.
The timings were merely to show how natively compiled execution is faster than the non-natively compiled execution. I was merely informing. I have no desire to tell others whether Dart is attractive or not. I'm not going to get dragged into another language war.

Heater
Posts: 14430
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 3:08 pm

OK. Fair enough.

Would be interesting to see the timing for fibo(42) nonetheless. Seeing as you have it set up and running.
Memory in C++ is a leaky abstraction .

hippy
Posts: 6709
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 10:19 pm

Code: Select all

                           Fibo(24)    Fibo(42)

Perl             5.28.1 :    0.300s  22m18.428s
MicroPython      1.11   :    0.261s  17m29.921s
Python           3.7.3  :    0.531s  15m42.762s
Python           2.7.16 :    0.265s  13m35.649s
Euphoria    interpreted :    0.183s   9m44.489s
Ruby                    :    0.495s   5m28.470s
Lisp (sbcl) interpreted :    0.155s   4m38.039s
Lua                     :    0.096s   4m00.754s
SOL                     :    0.025s   2m05.153s
GForth                  :    0.078s   1m53.154s
JavaScript       nodejs :    3.606s     41.798s
Dart           compiled :    0.064s     31.169s
Rust                    :    0.018s     23.504s
Pascal		    fpc :    0.011s     18.346s
C                       :    0.013s     17.311s
VB.Net             mono :    0.149s     16.060s
Euphoria       compiled :    0.014s     15.589s
Dart        interpreted :    4.298s     15.599s
Tiny C Compiler     tcc :    0.013s     15.301s
Swift       interpreted :    0.871s     14.749s
Go                      :    0.014s     14.746s
Julia                   :    1.006s     12.172s
C#                 mono :    0.168s     10.887s
Rust          optimised :    0.013s      8.476s
C             optimised :    0.008s      5.022s
Last edited by hippy on Tue Feb 18, 2020 8:30 pm, edited 6 times in total.

Heater
Posts: 14430
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dart Native on Raspberry Pi

Sat Dec 07, 2019 10:34 pm

Wow! That is above and beyond the call of duty. Brilliant.

Now we can tell how much global warming we are causing with different languages :)
Memory in C++ is a leaky abstraction .

User avatar
Paeryn
Posts: 2805
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Dart Native on Raspberry Pi

Sun Dec 08, 2019 12:44 am

What is Dart doing in the interpreted version that makes it twice as fast as the compiled version for Fibo(42)?
hippy wrote:
Sat Dec 07, 2019 10:19 pm

Code: Select all

                         Fibo(24)    Fibo(42)

Dart         compiled :    0.064s     31.169s
Dart      interpreted :    4.298s     15.599s
She who travels light — forgot something.

hippy
Posts: 6709
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Dart Native on Raspberry Pi

Sun Dec 08, 2019 1:53 am

Paeryn wrote:
Sun Dec 08, 2019 12:44 am
What is Dart doing in the interpreted version that makes it twice as fast as the compiled version for Fibo(42)?
It is a bit of a mystery; surprised me as well. I've more than double-checked because I couldn't believe it either ...

Code: Select all

[email protected]:~ $ cat fibo.dart
int Fibonacci(int n)
{
  if (n <= 1) { return n;                               }
  else        { return Fibonacci(n-1) + Fibonacci(n-2); };
}

main()
{
  print(Fibonacci(42));
}
[email protected]:~ $ time ./dart-sdk/bin/dart fibo.dart
267914296

real    0m15.678s
user    0m17.135s
sys     0m0.351s
[email protected]:~ $ time ./dart-sdk/bin/dart2native fibo.dart
Generated: /home/pi/fibo.exe

real    0m36.078s
user    0m41.384s
sys     0m1.888s
[email protected]:~ $ time ./fibo.exe
267914296

real    0m31.182s
user    0m31.158s
sys     0m0.021s
[email protected]:~ $ 
My best guess is the interpreter 'sets up a big stack', hence the slow start-time, but the executable allocates on demand; faster to start, slower to complete.

User avatar
Paeryn
Posts: 2805
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Dart Native on Raspberry Pi

Sun Dec 08, 2019 2:29 am

I thought you'd accidentally swapped them or something. I might have to download it and trace it.
She who travels light — forgot something.

hippy
Posts: 6709
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Dart Native on Raspberry Pi

Sun Dec 08, 2019 3:14 pm

There's some nice Fibonacci-like growth in execution times for compiled, slightly less when interpreted -

Code: Select all

Dart    Interpreted  Compiled

Fibo(24)     4.298s    0.064s
Fibo(25)     -         -
Fibo(26)     -         -
Fibo(27)     -         -
Fibo(28)     -         -
Fibo(29)     -         -
Fibo(30)     -         -
Fibo(31)     -         -
Fibo(32)     -         -
Fibo(33)     4.415s    0.502s
Fibo(34)     -         -
Fibo(35)     -         -
Fibo(36)     4.863s    1.846s
Fibo(37)     5.317s    2.951s
Fibo(38)     5.964s    4.653s
Fibo(39)     6.956s    7.457s
Fibo(40)     8.624s   12.006s
Fibo(41)    11.302s   19.426s
Fibo(42)    15.599s   31.169s
Fibo(43)    22.773s   51.096s
Fibo(44)    34.281s   82.066s
Interpreted on the left, compiled on the right -
GRAPH.jpg
GRAPH.jpg (8.21 KiB) Viewed 1335 times

Heater
Posts: 14430
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dart Native on Raspberry Pi

Sun Dec 08, 2019 11:08 pm

Interesting.

A log-log plot would be nice to see.
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”