Re: 0-based Indexing
- Posted by Derek Parnell <ddparnell at bigpond.com> Jul 17, 2003
- 532 views
On Thu, 17 Jul 2003 12:33:38 +1000 (07/17/03 12:33:38) , <dm31 at uow.edu.au> wrote: > > >> From: "C. K. Lester" <cklester at yahoo.com> > >> Derek splained (snippage occurred): >> >>> > I don't get it. Why do some languages use 0-based > indexing? It's got to >> be >>> > the dumbest thing in programming language history. Or > maybe not. >>> > >>> The index is actually an offset from an address. >> >> Okay, I get that... and that's reasonable for the behind-the- > scenes data >> manipulation, but why not hide that detail and use 1-based > indexing? Is it >> that much of a performance issue? Like Greg said, 1-based > indexing is more >> intuitive, easier to read and understand... > > Well, for me personally (maybe because I'm use to programming in > C/C++/ASM) 0-based is easier and more understandable. Also, for example. > say I want to use 1 byte of storage as my index var. If I start at 0, I > get from 0-255 different elements, whereas if I used 1-base then I could > only get 1- > 255. Spoken like a true C coder. This is Euphoria - we don't have byte, words and doublewords any more, just atom and sequences. Using an atom (the smallest unit) we can access billions of elements, so losing one possible value is not so tough. Also, an index with a value of zero now has meaning in Euphoria; its not got a usable value in it. > Having 0-based makes some algo's cleaner to write, and more effiecent. > (in above example use a 256 length array, I only need 1 byte for 0-based, > and 2 bytes for 1-based. Not much of a difference, but I'm a large > program with many of those it would) How many are really needed to 'make a difference'? Hundreds, Thousands, Millions? Typically, a complex program might have a few thousand variables, so lets say something like 5000 simultaneously used index variables. Comparing a 1-byte per index to Euphoria's atoms, we could save 15000 bytes of RAM ( 0.14% of a megabyte) - hmmmmm not too much nowadays. Not to mention that on average, modern CPUs process 32-bit integers faster than 8- bit integers due to accessing 32-bit aligned data faster than off-aligned data. As for algorithms, I agree that some algorithms are better suited to offsets (C-indexes) rather than ordinals (Eu-indexes). And some aren't. Also, some algorithms work better with ordinals than offsets. -- A method of removing one sequence from another. -- Euphoria's 1-based indexing n = find(a,b) if n then b = b[1..n-1] & a[n + length(a) - 1 .. length(b)] end if -- C's 0-based indexing n = find(a,b) if n >= 0 then b = b[0..n-1] & a[n + length(a) .. length(b) - 1] end if As you can see, not much of a difference really. However, the language called 'D' has slicing too, but its slices exclude the last element referenced. Thus in D (which is a zero-based index system, BTW) you would code... n = find(a,b) if n >= 0 then b = b[0..n] & a[n + length(a) .. length(b)] end if which is a difference. The upshot is, work with the language you've got. One can still always find room for improvements though -- cheers, Derek Parnell