From 2a4113847867ccaff71ada3a38e84f26ca0007f8 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 21 May 2023 01:54:03 -0400 Subject: [PATCH] tweaks. --- README.md | 28 ++++++++++++++++++++-------- nim/makefile | 1 + nim/samples | Bin 116256 -> 116168 bytes time.txt | 23 +++++++++++++++-------- 4 files changed, 36 insertions(+), 16 deletions(-) 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 dd0e138d08f595afdab8e69e499bfb9de928572e..63b56c41d3148e02bd8ea5c234a8bbfbfc11fee9 100755 GIT binary patch delta 6181 zcmZXYeRR$D9ml`l8xar1y&nCco;f*Z{}$p zn-Kj(Ro$pWR%XeJY&$wPhN2G=r$blj+L_Z+ZAr8p&TRJi{yy)YHFv*1^1HwH>+|$| z{{0f!oOEVWQgJ`WnYU{OJd@DrpUL-UjxNZXc;fcG^vTPwp4}K8-D361gxZw}`bwcb zO<7BI7oA3h<`$CMW$Wf1)IzBis_CJMyH-*I8@u;2b||L`qzNAZvue^mXZ#^>I9K!v z;2Y#KxJ9CPdzXqH;%9RWq+Ek@Y#g>!W#eU3V(@MoZ)%QDQXS)Q9|8Ms3oe$M`WNe3 zB`Z&cwv>L*yR{TOHmtcml2Vs(wrLjbu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AjVZ%QUU)7G<| zpRuk4CtWS&1=$9pnhK#&u~kROxrT>u8rX|H^)O<@D2uUfv8?)Z=2K@Xh@t{}WA0dEE4BWI+^j$bGXUNCccMUlB znBdhoqx|QERj&JX+X)*}yZ zdU$KB<%#=1jCm(;FDIF=E|alguo!&|V>j#8%A!zv)2BW;r!;?!;|uEO+=m(u`dnSl&HVa@_UsH_I*jC4##*x7|fu9+0GwxCI>s zU2Sb^KZ$iS-z^VGb!SjyfwAYesq)IkuG|^9Baqf^_ z$3yAs5gd43@Is7qBy=w>(=LcQ33}|D2)xa$C!g{;oNGOB@MDpiwX{*HH25vXW+EbS zL-~Xn8E5_nxB=Lnr__EjLGf?D2UwSTa)kNCqf8ko`S9*l<5@QePsK%?L%Hj@p83E{ zak!L<4L%I)p6#&0`X#u!M74|)TNqpE^E>v(E{r=cy`Tr<0%TcwstVeF_hXu#W8FGn zeMRVwzrPA8tAguUhj8g}Y4%W&!EeG_;R|CE$~{iijKf!fy|{YT0xRxYb2mnW!!gLeTp;0q)h_-!gO_be;52`8D1E z)@Dfen=N{P`8A^MsjWI>`31sW_?$GP{H#+sj7pG@W&C6T3 zbm5|z^YSuBWR9hZUHVbu`Y6@!)ai6>r!K!2&eFP2*WEg~_kx*4i)Sue^z_2)*^B4@ zV&T0Y85tw_BP_~)mJZ*okKS{gR(EB$&e!+)R%JcvChXP6yVoTi(=Q}B`V6ANcl9WJ Ki>lw%1OErZT|Z?2 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