Why is range(0) == range(2, 2, 2) True in Python 3?

The range objects are special: Python will compare range objects as Sequences. What that essentially means is that the comparison doesn’t evaluate how they represent a given sequence but rather what they represent. The fact that the start, stop and step parameters are completely different plays no difference here because they all represent an empty … Read more

built-in range or numpy.arange: which is more efficient?

For large arrays, a vectorised numpy operation is the fastest. If you must loop, prefer xrange/range and avoid using np.arange. In numpy you should use combinations of vectorized calculations, ufuncs and indexing to solve your problems as it runs at C speed. Looping over numpy arrays is inefficient compared to this. (Something like the worst … Read more

How to check if an integer is within a range?

Tested your 3 ways with a 1000000-times-loop. t1_test1: ($val >= $min && $val <= $max): 0.3823 ms t2_test2: (in_array($val, range($min, $max)): 9.3301 ms t3_test3: (max(min($var, $max), $min) == $val): 0.7272 ms T1 was fastest, it was basicly this: function t1($val, $min, $max) { return ($val >= $min && $val <= $max); }

How to know if today’s date is in a date range?

In Ruby 1.9.2 === doesn’t work, I get an error: irb(main):019:0> (Time.now .. (Time.now+1)) === Time.now TypeError: can’t iterate from Time from (irb):19:in `each’ from (irb):19:in `include?’ from (irb):19:in `include?’ from (irb):19:in `===’ from (irb):19 from /opt/ruby192/bin/irb:12:in `<main>’ Instead use #cover?: irb(main):002:0> (Time.now..Time.now+4).cover?(Time.now) => true irb(main):003:0> (Time.now..Time.now+4).cover?(Time.now+10) => false