Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Created August 17, 2016 20:02
Show Gist options
  • Save PetarKirov/c87772c4b1e72c4cf96a3d4918eb7580 to your computer and use it in GitHub Desktop.
Save PetarKirov/c87772c4b1e72c4cf96a3d4918eb7580 to your computer and use it in GitHub Desktop.
import std.experimental.ndslice;
string genCode(size_t n)
{
import std.algorithm.iteration : map;
import std.format : format;
import std.range : iota;
import std.typecons : tuple;
return "result = sl.structure.strides[$-1] == 1 &&
%(%(sl.structure.strides[%s] == sl.structure.lengths[%s] * sl.structure.strides[%s]%)%| && \n%);"
.format(iota(1, n).map!(i => tuple(i - 1, i, i)));
}
pragma (msg, genCode(13));
pragma (inline, false);
bool checkslice_1(SL)(const ref SL sl)
{
bool result = sl.structure.strides[$-1] == 1;
//mixin (genCode(SL.N));
foreach (idx; 1 .. SL.N)
result = result && (sl.structure.strides[idx - 1] ==
sl.structure.lengths[idx] * sl.structure.strides[idx]);
return result;
}
pragma (inline, false);
bool checkslice_2(SL)(const ref SL sl)
{
bool result;
mixin (genCode(SL.N));
return result;
}
pragma (inline, false);
bool checkslice_3(SL)(const ref SL sl)
{
import std.typecons : staticIota;
if (sl.structure.strides[$-1] != 1)
return false;
foreach (idx; staticIota!(1, SL.N))
if (sl.structure.strides[idx - 1] !=
sl.structure.lengths[idx] * sl.structure.strides[idx])
goto False;
return true;
False: return false;
}
pragma (inline, false);
bool checkslice_4(SL)(const ref SL sl)
{
import std.typecons : staticIota;
if (sl.structure.strides[$-1] != 1)
return false;
foreach (idx; staticIota!(1, SL.N))
if (sl.structure.strides[idx - 1] !=
sl.structure.lengths[idx] * sl.structure.strides[idx])
return false;
return true;
}
void main()
{
import std.array : array;
import std.range : iota;
import std.typecons : Yes;
auto sl = iota(0, 5UL ^^ 12).array.sliced!(Yes.replaceArrayWithPointer)
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
import std.datetime : benchmark, to;
import std.stdio : writefln;
auto res = benchmark!(
() { checkslice_1(sl) || assert (0); },
() { checkslice_2(sl) || assert (0); },
() { checkslice_3(sl) || assert (0); },
() { checkslice_4(sl) || assert (0); },
)(10_000_000UL);
foreach (i, r; res)
writefln("checkslice_%s -> %.3f", i + 1, r.to!("msecs", float));
writefln("\n\n RT:\n%s", genCode(10));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment