zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

some D code #135

Open zzz6519003 opened 4 years ago

zzz6519003 commented 4 years ago

D is a general-purpose programming language with static typing, systems-level access, and C-like syntax. With the D Programming Language, write fast, read fast, and run fast.

zzz6519003 commented 4 years ago

Fast code, fast.

zzz6519003 commented 4 years ago

import std.stdio, std.array, std.algorithm;

void main()
{
    stdin
        .byLineCopy
        .array
        .sort!((a, b) => a > b) // descending order
        .each!writeln;
}
zzz6519003 commented 4 years ago

Array in paraelle


void main()
{
    import std.datetime.stopwatch : benchmark;
    import std.math, std.parallelism, std.stdio;

    auto logs = new double[100_000];
    auto bm = benchmark!({
        foreach (i, ref elem; logs)
            elem = log(1.0 + i);
    }, {
        foreach (i, ref elem; logs.parallel)
            elem = log(1.0 + i);
    })(100); // number of executions of each tested function
    writefln("Linear init: %s msecs", bm[0].total!"msecs");
    writefln("Parallel init: %s msecs", bm[1].total!"msecs");
}