Skip to content

Instantly share code, notes, and snippets.

@Jura-Z
Created December 29, 2018 07:23
Show Gist options
  • Save Jura-Z/f196566cf6f3ec60a19fb382172fa738 to your computer and use it in GitHub Desktop.
Save Jura-Z/f196566cf6f3ec60a19fb382172fa738 to your computer and use it in GitHub Desktop.
Pythagorean Triples, iota/ranges
import std.stdio;
import std.range;
import std.typecons;
import std.algorithm;
import std.concurrency : Generator, yield;
import core.time;
alias Triple = Tuple!(int, "x", int, "y", int, "z");
void main()
{
auto before = MonoTime.currTime;
auto gen = new Generator!Triple({
iota(1, int.max).each!((z) {
iota(1, z+1).each!((x) {
iota(x, z+1).each!((y) {
if (x*x + y*y == z*z)
yield(Triple(x, y, z));
});
});
});
});
foreach (t; gen.take(100))
writefln("%s, %s, %s", t.x, t.y, t.z);
auto after = MonoTime.currTime;
Duration timeElapsed = after - before;
writefln("%s", timeElapsed);
}
@Jura-Z
Copy link
Author

Jura-Z commented Dec 29, 2018

Runtime:
2 ms and 506 μs

Compilation:
~ time dmd iota.d
dmd iota.d 0.47s user 0.13s system 98% cpu 0.620 total
~ time dmd iota.d -O -release -inline -boundscheck=off
dmd iota.d -O -release -inline -boundscheck=off 1.29s user 0.17s system 99% cpu 1.467 total

MacBook Pro (Retina, 15-inch, Mid 2015), 2,8 GHz Intel Core i7, 16 GB 1600 MHz DDR3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment