Created
August 17, 2016 21:46
-
-
Save PetarKirov/e84cede75aa0d207430aa80943fe84d5 to your computer and use it in GitHub Desktop.
This file contains 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
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; | |
bool result = sl.structure.strides[$-1] == 1; | |
foreach (idx; staticIota!(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_4(SL)(const ref SL sl) | |
{ | |
import std.typecons : staticIota; | |
if (sl.structure.strides[$-1] != 1) | |
goto 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_5(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); }, | |
() { checkslice_5(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
checkslice_1 -> 3876.754
checkslice_2 -> 45.732
checkslice_3 -> 42.872
checkslice_4 -> 46.207
checkslice_5 -> 42.872