Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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)
import std.algorithm, std.conv, std.datetime, std.functional, std.range, std.stdio, std.traits, std.typecons;
private void doNotOptimizeAway(T)(auto ref T t)
{
import core.thread : getpid;
import std.stdio : writeln;
if(getpid() == 1) {
writeln(*cast(char*)&t);
}
}
@wilzbach
wilzbach / result
Last active January 13, 2017 08:09
std.random.uniform vs. bitwise
> ldc -O5 -release -boundscheck=off test.d && ./test 1 2 8 256 1024 65536
benchmark: 1 (bits: 1)
random.uniform = 742 ms, 824 μs, and 5 hnsecs
random.bitwise = 218 ms, 972 μs, and 9 hnsecs
random.uniform.fixed = 592 ms, 647 μs, and 2 hnsecs
benchmark: 2 (bits: 2)
random.uniform = 746 ms, 839 μs, and 5 hnsecs
random.bitwise = 497 ms and 189 μs
random.uniform.fixed = 594 ms, 196 μs, and 1 hnsec
@wilzbach
wilzbach / fixer.d
Last active February 16, 2017 01:27
Fix indentation warnings for Phobos
#!/usr/bin/env rdmd
auto parenDiff(S)(S ss)
{
auto count = 0;
foreach (s; ss)
{
switch (s)
{
case '(': count++; break;