Last active
August 21, 2016 22:01
-
-
Save wilzbach/d9d2376a33ff52b22ee9046f8b20218a to your computer and use it in GitHub Desktop.
Flex.d vs Tinflex.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env dub | |
/+ dub.json: | |
{ | |
"name": "bench_flex", | |
"dependencies": { | |
"mir": {"path": "."}, | |
}, | |
"dflags-ldc": ["-mcpu=native"] | |
} | |
+/ | |
/+ | |
$ ldc-git --version | |
LDC - the LLVM D compiler (798cda): | |
based on DMD v2.071.2-b1 and LLVM 3.8.1 | |
built with DMD64 D Compiler v2.071.1 | |
Default target: x86_64-unknown-linux-gnu | |
Host CPU: haswell | |
http://dlang.org - http://wiki.dlang.org/LDC | |
+/ | |
import mir.random.flex; | |
__gshared double r; | |
void main() | |
{ | |
import std.stdio : writefln; | |
import std.conv : to; | |
alias S = double; | |
int n = 10_000; // number of samples | |
import std.random : Mt19937; | |
auto gen = Mt19937(42); | |
import std.math : exp, log, PI, sqrt; | |
S rho = 1.1; | |
S[] points = [-S.infinity, -1.5, 0, 1.5, S.infinity]; | |
enum S halfLog2PI = S(0.5) * log(2 * PI); | |
auto f0 = (S x) => -(x * x) * S(0.5) - halfLog2PI; | |
auto f1 = (S x) => -x; | |
auto f2 = (S x) => S(-1); | |
import std.datetime; | |
import std.stdio; | |
StopWatch sw; | |
sw.start(); | |
foreach (i; 0..n) | |
{ | |
auto f = flex(f0, f1, f2, -0.5, points, rho); | |
// we even sample a value on top to show that no optimizations are done | |
r = f(gen); | |
} | |
sw.stop(); | |
writeln(sw.peek().msecs, " ms"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> dub run --build=release-nobounds --compiler=ldmd2-git --single flex.d | |
n = 10K | |
1.1: 122 ms | |
n = 1K | |
1.01: 46 ms | |
1.001: 169 ms | |
> Rscript rinflex.R | |
n = 10K | |
1.1: 79.47334 secs ( intervals) | |
n = 1K | |
1.01: 21.22911 secs ( intervals) | |
1.001: 79.04718 secs ( intervals) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(Tinflex) | |
SEED <- 123456 | |
set.seed(SEED) | |
rho <- 1.1 | |
halfLog2PI <- 0.5 * log(2 * pi); | |
lf <- function(x) { -(x * x) * 0.5 - halfLog2PI } | |
dlf <- function(x) { -x } | |
d2lf <- function(x) { -1 } | |
cT <- -0.5 | |
n = 10000 | |
start.time <- Sys.time() | |
for (i in 1: n) { | |
gen <- Tinflex.setup(lf, dlf, d2lf, ib=c(-Inf,-1.5,0,1.5,3, Inf), cT=cT, rho=rho) | |
} | |
end.time <- Sys.time() | |
time.taken <- end.time - start.time | |
difftime(end.time, start.time, tz, units = "secs") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment