Reality drives me to write less clean code, sorry for mistakenly posting in D.announce

Bill Baxter dnewsgroup at billbaxter.com
Thu Oct 11 16:56:22 PDT 2007


davidl wrote:
> 
> First practice:
> 
> char[] text;
> char* p;
> char* end;
> 
> Version1:
> p=text.ptr;
> end= text.ptr+length;
> we use p to scan over the text, and end as a mark.
> 
> Version2:
> text[blah] to scan over the text.
> 
> Obviously version 2 is cleaner, but slower.

Don't be so sure.  Did you measure it?

And don't forget Version3:
foreach(c; text)
{ ... }
Supposedly generates code as efficient as a for loop.

> Second practice:
> 
> version 1:
> enum basictype
> {
>       tint,
>       tint32,
>       tunsint32,
> 
>       tlong,
>       tuslong32,
> 
>       tcomplex,
>       tcomplex64,
>       tcomplex80,
> 
>       timaginary,
>       timaginary64,
>       timaginary80,
> }
> 
> class Type
> {
>      basictype bt;
> }
> 
> 
> we do some switch-case to test what on earth this type is in this case.
> 
> version2:
> 
> class Type{}
> 
> interface TypeReal:TypeComplex{}
> interface TypeImaginary:TypeComplex{}
> interface TypeComplex{}
> interface Type32bit{}
> interface Type64bit{}
> interface Type80bit{}
> interface TypeUnsigned{}
> class TypeNumber:Type{}
> 
> class TypeUSInt:TypeNumber, Type32bit, TypeUnsigned{}
> class TypeInt:TypeNumber,Type32bit {}
> class TypeComplex64:TypeNumber,TypeComplex, Type64bit {}
> class TypeComplex80:TypeNumber,TypeComplex, Type80bit{}
> class TypeImaginary:TypeNumber,TypeImaginary {}
> class TypeImaginary64:TypeNumber,TypeImaginary, Type64bit{}
> 
> 
> Honestly, I prefer version2 a little bit more to version1.
> 
> But, version2 gets too much runtime penalty.
> 
> Ideas?

No, sorry.  Maybe you could explain your requirements a little more 
clearly.  The two examples look like they will do different things.

--bb



More information about the Digitalmars-d mailing list