move nim to top level, add to README

This commit is contained in:
NunoSempere 2023-05-21 01:46:45 -04:00
parent a84b6b9cc0
commit 2cf684da56
16 changed files with 38 additions and 16 deletions

View File

@ -13,29 +13,35 @@ The title of this repository is a pun on two meanings of "time to": "how much ti
- [x] Squiggle
- [x] Javascript (NodeJS)
- [x] C
- [x] Nim
## Performance table
With the [time](https://man7.org/linux/man-pages/man1/time.1.html) tool, using 1M samples:
| Language | Time |
|----------|-----------|
| C | 0m0,442s |
| Node | 0m0,732s |
| Squiggle | 0m1,536s |
| R | 0m7,000s |
| Python (CPython) | 0m16,641s |
| Language | Time |
|----------------------|-----------|
| Nim | 0m0.183s |
| C | 0m0,442s |
| Node | 0m0,732s |
| Squiggle | 0m1,536s |
| R | 0m7,000s |
| Python (CPython) | 0m16,641s |
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 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 [BoxMuller 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
- Julia (TuringML)
- Rust
- Lisp
- [ ] Julia (TuringML)
- [ ] Rust
- [ ] Lisp
- [ ] Stan
- [ ] Go
- [ ] Zig
- [ ] Forth
- ... and suggestions welcome
- Stan
## Roadmap

View File

@ -29,7 +29,7 @@ proc sine(x: float): float =
## Arithmetic-geomtric mean
proc ag(x: float, y: float): float =
let n = 16 # just some high number
let n = 32 # just some high number
var a = (x + y)/2.0
var b = sqrt(x * y)
for i in 0..n:
@ -41,7 +41,7 @@ proc ag(x: float, y: float): float =
## Find m such that x * 2^m > 2^precision/2
proc find_m(x:float): float =
var m = 0.0;
let precision = 32 # bits
let precision = 64 # bits
let c = pow(2.0, precision.float / 2.0)
while x * pow(2.0, m) < c:
m = m + 1

View File

@ -8,4 +8,5 @@ run: samples
./samples $(VERBOSE)
examine: samples
nim c $(VERBOSE) -d:release samples.nim && time ./samples $(VERBOSE)
nim c $(VERBOSE) -d:release samples.nim && time ./samples $(VERBOSE) && echo
nim c $(VERBOSE) -d:danger samples.nim && time ./samples $(VERBOSE)

BIN
nim/samples Executable file

Binary file not shown.

View File

@ -36,3 +36,18 @@ real 0m7,000s
user 0m6,944s
sys 0m0,052s
## Nim
nim c --verbosity:0 -d:release samples.nim && time ./samples --verbosity:0 && echo
0.8873815480558296
real 0m0.183s
user 0m0.150s
sys 0m0.032s
nim c --verbosity:0 -d:danger samples.nim && time ./samples --verbosity:0
0.8886260086130379
real 0m0.157s
user 0m0.136s
sys 0m0.020s

Binary file not shown.