Speed of horizontal flip

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 1 09:08:13 PDT 2015


On Wednesday, 1 April 2015 at 13:52:06 UTC, tchaloupka wrote:
<snip>

I'm pretty sure that the flipping happens in GDI+ as well. You 
might be writing C#, but the code your calling that's doing all 
the work is C and/or C++, quite possibly carefully optimised over 
many years by microsoft.

Are you even sure that your C# code truly performs a flip? It 
could easily just set the iteration scheme and return (like 
numpy.ndarray.T does, if you're familiar with python).

dmd does not produce particularly fast code. ldc and gdc are much 
better at that.

Sadly, std.algorithm.reserve isn't perhaps as fast as it could be 
for arrays of static arrays, at least in theory. Try this, but I 
hope that with a properly optimised build from ldc/gdc it won't 
make any difference:

void reverse(ubyte[3][] r)
{
     immutable last = r.length-1;
     immutable steps = r.length/2;
     foreach(immutable i; 0 .. steps)
     {
         immutable tmp = r[i];
         r[i] = r[last - i];
         r[last - i] = tmp;
     }
}

unittest
{
	ubyte[3] a = [1,2,3];
	ubyte[3] b = [7,6,5];
	
	auto c = [a,b];
	c.reverse();
	assert(c == [b,a]);
	
	ubyte[3] d = [9,4,6];
	
	auto e = [a,b,d];
	e.reverse();
	assert(e == [d,b,a]);
	
	auto f = e.dup;
	e.reverse;
	e.reverse;
	assert(f == e);
}


More information about the Digitalmars-d-learn mailing list