Array indexing and slicing

Luís Marques luismarque+spam at gmail.com
Tue Apr 4 05:49:08 PDT 2006


In article <e0tdh2$bsn$1 at digitaldaemon.com>, novice2 says...
>please, can anybody explain me, wich "performance penalty" mentioned in this
>thread? how it come?

When array bounds checking is disabled, all the CPU has to do is something like:

value = *(&array_contents + (index * element size));

The index is just added as an offset (times the element size, which can be
really fast if it's a multiple of 2). When you add bounds checking or negative
indexes, other operations must be performed. For bounds checking a conditional
jump must be issued (conditional jumps can be expensive for performance -- on
inner loops -- plus they add footprint). For negative indexes the corresponding
positive offset must be calculated before accessing the array index. Under some
circunstances the compiler could optimize that, when it would be possible to do
the calculation at compilation or to assume the index was a unsigned integer. I
suppose there's no general arithmetic algorithm for all cases (without
conditional jumps).

I guess that should be it, but others should know more about such matters.

Luís Marques



More information about the Digitalmars-d mailing list