diff --git a/README.md b/README.md index 33701a9c..222d7c03 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,29 @@ ## About -This repository contains example of very simple code to manipulate samples in various programming languages. As of now, it may be useful for checking the validity of simple estimations. +This repository contains example of very simple code to manipulate samples in various programming languages. It implements this estimate: -The title of this repository is a pun on two meanings of "time to": "how much time does it take to do x", and "let's do x". +``` +p_a = 0.8 +p_b = 0.5 +p_c = p_a * p_b + +dists = [0, 1, 1 to 3, 2 to 10] # each dist represented as 1M samples +weights = [(1 - p_c), p_c/2, p_c/4, p_c/4 ] + +result = mixture(dists, weights) +mean(result) +``` + +As of now, it may be useful for checking the validity of simple estimations. The title of this repository is a pun on two meanings of "time to": "how much time does it take to do x", and "let's do x". ## Current languages -- [x] Python -- [x] R -- [x] Squiggle -- [x] Javascript (NodeJS) - [x] C +- [x] Javascript (NodeJS) +- [x] Squiggle +- [x] R +- [x] Python - [x] Nim ## Performance table @@ -21,7 +33,7 @@ With the [time](https://man7.org/linux/man-pages/man1/time.1.html) tool, using 1 | Language | Time | |----------------------|-----------| -| Nim | 0m0.183s | +| Nim | 0m0.153s | | C | 0m0,442s | | Node | 0m0,732s | | Squiggle | 0m1,536s | @@ -30,7 +42,7 @@ With the [time](https://man7.org/linux/man-pages/man1/time.1.html) tool, using 1 I was very surprised that Node/Squiggle code was almost as fast as the raw C code. For the Python code, it's possible that the lack of speed is more a function of me not being as familiar with Python. It's also very possible that the code would run faster with [PyPy](https://doc.pypy.org). -I was also really happy with trying [Nim](https://nim-lang.org/). The version which beats all others is just the normal usage of Nim, in the "release" compilation mode (the "danger" compilation mode shaves of a few more miliseconds). The Nim version has the particularity that I define the normal function from scratch, using the [Box–Muller transform](https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform#Basic_form). For Nim I also have a version of the code which takes around 4 seconds, where I define some very inefficient sine & logarithm functions to feed into the Box-Muller method, because it felt like fun to really write a botec tool really from scratch. +I was also really happy with trying [Nim](https://nim-lang.org/). The version which beats all others is just the fastest "danger" compilation of Nim (the "release" compilation is 0m0.183s instead). The Nim version has the particularity that I define the normal function from scratch, using the [Box–Muller transform](https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform#Basic_form). For Nim I also have a version of the code which takes around 4 seconds, where I define some very inefficient sine & logarithm functions to feed into the Box-Muller method, because it felt like fun to really write a botec tool really from scratch. ## Languages I may add later diff --git a/nim/makefile b/nim/makefile index 2bdcc9ff..5836c8ca 100644 --- a/nim/makefile +++ b/nim/makefile @@ -8,5 +8,6 @@ run: samples ./samples $(VERBOSE) examine: samples + nim c $(VERBOSE) samples.nim && time ./samples $(VERBOSE) && echo nim c $(VERBOSE) -d:release samples.nim && time ./samples $(VERBOSE) && echo nim c $(VERBOSE) -d:danger samples.nim && time ./samples $(VERBOSE) diff --git a/nim/samples b/nim/samples index dd0e138d..63b56c41 100755 Binary files a/nim/samples and b/nim/samples differ diff --git a/time.txt b/time.txt index 37102e19..920d0789 100644 --- a/time.txt +++ b/time.txt @@ -38,16 +38,23 @@ sys 0m0,052s ## Nim -nim c --verbosity:0 -d:release samples.nim && time ./samples --verbosity:0 && echo -0.8873815480558296 +nim c --verbosity:0 samples.nim && time ./samples --verbosity:0 && echo +0.8881633539025908 -real 0m0.183s -user 0m0.150s +real 0m0.706s +user 0m0.685s +sys 0m0.020s + +nim c --verbosity:0 -d:release samples.nim && time ./samples --verbosity:0 && echo +0.8861663545062978 + +real 0m0.184s +user 0m0.151s sys 0m0.032s nim c --verbosity:0 -d:danger samples.nim && time ./samples --verbosity:0 -0.8886260086130379 +0.8879220244477399 -real 0m0.157s -user 0m0.136s -sys 0m0.020s +real 0m0.158s +user 0m0.130s +sys 0m0.028s