Skip to content

Instantly share code, notes, and snippets.

@wilzbach
wilzbach / minmax_benchmark.d
Created December 27, 2016 06:38
MinMax benchmark vs. reduce
import std.stdio;
import std.range;
import std.datetime;
import std.conv;
import std.algorithm;
import std.traits;
import std.typecons;
import std.functional;
private void doNotOptimizeAway(T)(auto ref T t)
@wilzbach
wilzbach / flex.d
Last active August 21, 2016 22:01
Flex.d vs Tinflex.R
#!/usr/bin/env dub
/+ dub.json:
{
"name": "bench_flex",
"dependencies": {
"mir": {"path": "."},
},
"dflags-ldc": ["-mcpu=native"]
}
+/
@wilzbach
wilzbach / pdf.ipynb
Created August 9, 2016 21:31
PDF & CDF
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@wilzbach
wilzbach / main.d
Last active August 9, 2016 02:10
FP precision with sin
void main() {
import std.stdio;
import std.meta : AliasSeq;
import std.math : PI, sin;
import core.stdc.math : sin_c = sin;
foreach (S; AliasSeq!(float, double, real))
{
S pi = PI;
writefln("x: %12s, x_c: %12s, x_c_pi: %12s", sin(S(PI)), sin_c(S(PI)), sin_c(pi));
}
@wilzbach
wilzbach / glob.d
Created August 7, 2016 16:14
Globbing in D
string[] glob(string pattern)
{
import std.string;
string[] results;
glob_t glob_result;
glob(pattern.toStringz, 0, null, &glob_result);
for (uint i = 0; i < glob_result.gl_pathc; i++)
{
results ~= glob_result.gl_pathv[i].fromStringz().idup;
}
@wilzbach
wilzbach / dmd -inline -release -O -boundscheck=off test.d
Last active August 5, 2016 11:15
std.math vs. core.stdc.math vs. intrinsics
fun: pow
std.math.pow = 13 secs, 743 ms, 902 μs, and 7 hnsecs
core.stdc.pow = 12 secs, 490 ms, 213 μs, and 2 hnsecs
fun: exp
std.math.exp = 6 secs, 905 ms, and 644 μs
core.stdc.exp = 16 secs, 336 ms, 330 μs, and 4 hnsecs
fun: exp2
std.math.exp2 = 3 secs, 338 ms, 447 μs, and 9 hnsecs
core.stdc.exp2 = 5 secs, 244 ms, 528 μs, and 6 hnsecs
fun: sin
@wilzbach
wilzbach / main.d
Last active August 5, 2016 00:17
setBit at runtime and compile-time
auto setBit(ubyte idx)(ulong bitfield)
{
enum mask = 1UL << idx;
return bitfield |= mask;
}
ulong setBit(ulong bitfield, ulong idx)
{
ulong mask = 1UL << idx;
return bitfield |= mask;
@wilzbach
wilzbach / main.d
Created August 1, 2016 15:08
FP magic fun on 32-bit
S fun(S)(in S x)
{
return -1 / x;
}
void main()
{
alias S = double; // same for float
S i = fun!S(3);
assert(i == S(-1) / 3); // this lines passes
@wilzbach
wilzbach / main.d
Created August 1, 2016 06:50
FP magic with 32-bit
import std.stdio;
alias S = float;
float shL = 0x1.1b95eep-4; // -0.069235
float shR = 0x1.9c7cfep-7; // -0.012588
F fun(F)(F x)
{
return 1.0 + x * 2;
}
@wilzbach
wilzbach / main.d
Created August 1, 2016 05:57
std.math.pow vs C pow
void main()
{
alias S = float;
S s1 = 0x1.24c92ep+5;
S s2 = -0x1.1c71c8p+0;
import std.math : std_pow = pow;
import core.stdc.stdio : printf;
import core.stdc.math: powf;