Method template optimization that works in C++, but not very well D

Vladimir v.s.zverev at gmail.com
Wed Feb 19 07:13:07 PST 2014


I`m learning templates in D language and I want to thank the 
creators of the language for this.
  	
There is an educational task: process table entries, if you know 
which column to use.

Simple way to do this:
int ProcessTable(.....)
{
     int result = 0;

     foreach (ref row; table)
         result += ProccessRow(row, isUseField);

     return result;
}

int ProccessRow(.....)
{
     ....
     foreach (i, value; TableRow.Fields)
         if (isUseField[i])
            //doing something
     ....
}

In c++ I can create in compile-time special vesrion ProccessRow 
function for various combinations of the array "isUseField" 
values. This reduces the total number of run-time operations in 
most cases.

I implemented such idea, but result confused me. I used several 
compilers: dmd(version: DMD32 D Compiler v2.063.2, options: 
-inline -release -O -noboundscheck), ldmd (version: the LLVM D 
compiler (67a1a3)  based on DMD v2.063.2 and LLVM 3.2svn; 
options: -inline -release -O -noboundscheck) and gdc (version: 
4.8.1, options: -O3 -march=native -frelease -fno-bounds-check).

     Result dmd : simple processing - 39 ms; template processing - 
20 ms.
     Result ldmd: simple processing -  8 ms; template processing - 
12 ms.
     Result gdc : simple processing -  8 ms; template processing - 
14 ms.

Where I'm wrong? How  I  can rewrite the program to make effect 
more evident? Or may be, this optimization is meaningless in D...?

Full version my very simplified example such task: 
https://gist.github.com/Vladimir-Z/1a1755ce91cb0e7636b5

Thanks for your advice.


More information about the Digitalmars-d-learn mailing list