Array indexing and slicing
xs0
xs0 at xs0.com
Wed Apr 5 04:06:01 PDT 2006
novice2 wrote:
>>> i just want to say, IMHO, all changes in index/slicing syntax will change
>>> compiler only. not generated binary.
>> Actually I don't think that can be true. That would only be the case if the
>> indexes were defined at compilation time. Assuming positive indexes only, the
>> following code...
>
> but programmer defines algo - sequence of some operations.
> we should compare equal code for currnet syntax and new syntax.
> equal code - with equal results.
>
> just imaginary example:
>
> task: get 3rd item from end in array;
> new synatx: b = a[-3];
> old syntax: b = a[length-3];
>
> only now we can compare, wich actions will be performed in runtime,
> and guess, will be it different or not.
"old" syntax is actually
b = a[$-3];
But that is not a good example, because the index is a constant. A
better example would be:
int idx=someFunc();
b=a[idx];
It currently compiles to something like
int idx=someFunc();
debug {
if (idx<0 || idx>=a.length)
throw ...;
}
b=a[idx];
If you use -release, the debug {} section is eliminated and there is no
cost in speed. On the other hand, with the new version, the result would
be something like
int idx=someFunc();
if (idx<0) {
idx+=a.length;
}
debug {
if (idx<0 || idx>=a.length)
throw ...;
}
b=a[idx];
The problem is that in the general case, the compiler cannot determine
whether idx will be negative or not, and cannot optimize away the first
if() statement, meaning every array access will be notably slower, even
if you don't use negative indices at all.
Is that inefficiency worth the change? I don't think it is...
xs0
More information about the Digitalmars-d
mailing list