language: D (dmd) (dmd-2.042)
date: 834 days 8 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import std.c.stdio: printf;
import std.date: ticksPerSecond, getUTCtime;
import std.system: endian, Endian;
 
void convert1(ubyte[] arr) {
    int len = arr.length;
    for (int i = 0; i < len; i += 4) {
        ubyte b = arr[i + 0];
        ubyte g = arr[i + 1];
        ubyte r = arr[i + 2];
        ubyte a = arr[i + 3];
        arr[i + 0] = a;
        arr[i + 1] = b;
        arr[i + 2] = g;
        arr[i + 3] = r;
    }
}
 
void convert2(ubyte[] arr)
    in {
        assert(!(arr.length % uint.sizeof));
    } body {
        if (endian == Endian.LittleEndian)
            foreach (ref x; cast(uint[])arr)
                x = (x << 8) | (x >> 24);
        else
            foreach (ref x; cast(uint[])arr)
                x = (x >> 8) | (x << 24);
    }
 
void main() {
    enum int N = 100;
 
    auto a = new ubyte[1_920_000];
    foreach (i, ref x; a)
        x = cast(ubyte)(i % ubyte.max);
 
    auto a1 = a.dup;
    auto a2 = a.dup;
    assert(a1 == a2);
    convert1(a1);
    convert2(a2);
    assert(a1 == a2);
 
    auto t0 = getUTCtime();
    for (int i; i < N; i++)
        convert1(a);
    auto t1 = getUTCtime();
    printf("%lf\n", (t1-t0) / cast(double)ticksPerSecond);
 
    t0 = getUTCtime();
    for (int i; i < N; i++)
        convert2(a);
    t1 = getUTCtime();
    printf("%lf\n", (t1-t0) / cast(double)ticksPerSecond);
}