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

Ali Çehreli acehreli at yahoo.com
Wed Feb 19 11:30:40 PST 2014


On 02/19/2014 07:13 AM, Vladimir wrote:

 > 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.

[...]

 > 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.

Can you show the C++ code perhaps on a simpler task? I would expect D's 
compile-time features to be superior to C++'s in all cases. (And I would 
like to be educated if it is not so. :) )

 > 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?

I would like to understand the request first. Neither ProccessRow() nor 
OptimizedProcessTable() take isUseField as template parameters. Besides, 
isUseField is generated at runtime. No, I don't understand. :)

 > Or may be, this optimization is meaningless in D...?

I doubt it.

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

Ali



More information about the Digitalmars-d-learn mailing list