Optimize my code =)
Chris Williams
yoreanon-chrisw at yahoo.co.jp
Mon Feb 17 17:47:54 PST 2014
On Friday, 14 February 2014 at 16:00:09 UTC, Robin wrote:
> this(size_t rows, size_t cols) {
> this.dim = Dimension(rows, cols);
> this.data = new T[this.dim.size];
> enum nil = to!T(0);
> foreach(ref T element; this.data) element = nil;
> }
Your foreach here is unnecessary. You can zero out an array using
the syntax:
this.data[] = 0; // probably compiles out as memset()
And while I would expect to!T(0) to compile down to something
with no particular logic, it still is a function call not a
macro, so you're better to let the compiler figure out the best
conversion from 0 for type T than to use a library function for
it.
> I think and hope that this is getting optimized via inlining. =)
> This works similar for opIndexAssign.
From complaints that I have seen, very little gets inlined at the
moment.
> Matrix opMul(ref const(Matrix) other) {
> if (this.dim.rows != other.dim.cols || this.dim.cols !=
> ther.dim.rows) {
> // TODO - still have to learn exception handling first ...
> }
> auto m = new Matrix(this.dim.rows, other.dim.cols);
> auto s = new Matrix(other).transposeAssign();
Since you don't return s, it's probably better to not allocate an
instance. I would also be curious to see your constructor which
copies one matrix into a new one.
> Besides that I can't find a way how to make a use of move
> semantics like in C++. For example a rvalue-move-constructor or
> a move-assign would be a very nice thing for many tasks.
Do you mean something like this?
this.data[] = other.data[]; // probably compiles out as memcpy()
this.data = other.data.dup; // different syntax, but should be
the same
> Another nice thing to know would be if it is possible to
> initialize an array before it is default initialized with
> T.init where T is the type of the array's fields.
I believe so, but I would have to scour about to find the syntax.
Hopefully, someone else knows.
More information about the Digitalmars-d-learn
mailing list