Stupid little iota of an idea

Daniel Gibson metalcaedes at gmail.com
Wed Feb 9 12:03:57 PST 2011


Am 09.02.2011 20:57, schrieb bearophile:
> Daniel Gibson:
> 
>> Don't know about Python, but in D this will only be true if X is an integer.
>> I guess 1<X<4 is true for X=1.5 in Python.. it would certainly not be true for X
>> in 1..4 in D.
> 
> You are right, the semantics is different, my silly mistake. Thank you.
> 
> 
>> Also using X in 1..4 is in D is pretty bad if you just want to check if 1<X<4
>> (or even more when checking 1<X<100) because it has a much higher overhead -
>> even though it may be technically O(1) because 4 or 100 is a constant)
> 
> Even if the bounds are not constants it's not hard to perform  x in a..b  in O(1).
> 

If the compiler does optimizations (=> transforms it to if( a<=x && x<b)), yes.
If a..b are handled as a generic range (just like e.g. [1, 10, 4, 7, 3, 42]) I
don't think it's as easy in constant time. Maybe when putting the range into a
hash table.. but this also has some overhead.

> Bye,
> bearophile

Cheers,
- Daniel


More information about the Digitalmars-d mailing list