Optimize my code =)

John Colvin john.loughran.colvin at gmail.com
Mon Feb 17 02:27:02 PST 2014


On Monday, 17 February 2014 at 09:48:49 UTC, Robin wrote:
> Hiho,
>
> I currently haven't got enough time to respond to all what have 
> been said since my last post.
> However, here is the simple code:
> http://dpaste.dzfl.pl/3df8694eb47d
>
> Thanks in advance!
> I am really looking forward to your editing! =)
>
> Robin

A few quick points:

1) foreach loops are nice, use them

2) D has neat array-ops that can simplify your code for == and -= 
etc...
e.g.

	/**
	 * Subtracts all entries of this matrix by the given other 
matrix.
	 */
	ref Matrix opSubAssign(ref const(Matrix) other) nothrow
	in
	{
		assert(this.dim == other.dim);
	}
	body
	{
		this.data[] -= other.data[];
		return this;
	}


3) Although your generic indexing is nice, a lot of operations 
that you have implemented have access patterns that allow faster 
indexing without a multiplication, e.g.

	/**
	 * Creates a new identity matrix with the given size specified 
by the given rows and
	 * columns indices.
	 */
	static Matrix identity(size_t rows, size_t cols) nothrow {
		auto m = Matrix(rows, cols);
		size_t index = 0;
		foreach (i; 0 .. m.dim.min) {
			m.data[index] = 1;
			index += cols + 1;
		}
		return m;
	}


More information about the Digitalmars-d-learn mailing list