I'm currently investigating the difference of speed between references and copies. And it seems that copies got a immense slowdown if they reach a size of >= 20 bytes. In the code below you can see if my struct has a size of < 20 bytes (e.g. 4 ints = 16 bytes) a copy is cheaper than a reference. But with 5 ints (= 20 bytes) it gets a slowdown of ~3 times. I got these results:

16 bytes:
by ref: 49
by copy: 34
by move: 32

20 bytes:
by ref: 51
by copy: 104
by move: 103

My question is: why?

My system is Win 8.1, 64 Bit and I'm using dmd 2.067.1 (32 bit)

Code:

import std.stdio;
import std.datetime;

struct S {
    int[4] values;
}

pragma(msg, S.sizeof);

void by_ref(ref const S s) {

}

void by_copy(const S s) {

}

enum size_t Loops = 10_000_000;

void main() {
    StopWatch sw;

    sw.start();
    for (size_t i = 0; i < Loops; i++) {
        S s = S();
        by_ref(s);
    }
    sw.stop();

    writeln("by ref: ", sw.peek().msecs);

    sw.reset();

    sw.start();
    for (size_t i = 0; i < Loops; i++) {
        S s = S();
        by_copy(s);
    }
    sw.stop();

    writeln("by copy: ", sw.peek().msecs);

    sw.reset();

    sw.start();
    for (size_t i = 0; i < Loops; i++) {
        by_copy(S());
    }
    sw.stop();

    writeln("by move: ", sw.peek().msecs);
}

Reply via email to