last day (15 days later) » 

22:35
0
A: python3 - using for loop in a if condition

Stefan PochmannI like doing such all-in-range checks like this: 0 <= min(nums) <= max(nums) <= 255 That's usually faster. Measuring a little: >>> from timeit import timeit >>> timeit('0 <= min(nums) <= max(nums) <= 255', 'nums = range(256)') 10.911965313750706 >>> timeit('all(0 <= n <= 255 for n in nums)',...

Interesting, given that both min and max have to iterate the collection. Algorithmically 0 <= min(nums) and max(nums) <= 255 should be better..
@schwobaseggl Well, the iterations are simpler (only one comparison for each value), there's no generator overhead, and no Python code runs for each value. I just measured all([0 <= n for n in nums]) and that took 12.7 seconds.
@schwobaseggl Here I did, yes. Python 3.6.1. Why?
About your "and should be better" and also about all having the advantage of being able to stop early: The reason I do this stuff is to catch mistakes in data for coding contests, to ensure that all test data is valid. Which means that I fully expect all values to be in the range and thus fully expect having to run all tests. And I suspect that's quite usual, not just for me and my use cases.
Interesting again, the timings don't care whether range(x) or list(range(x)) is used...
Yup, of course. For data where most is outside the range, all is much faster
@schwobaseggl Ha, well, if it were constant time, then it wouldn't be only twice as fast :-). Also, how would the range object speed that up? There's no __min__ and __max__ that min/max could use, is there?
True, I was just thinking about that. The range object would be a perfect candidate for __min__ and __max__ :)
22:39
Or I guess min/max could check whether the given source is a range. But that would need extra code and would add a tiny amount of time to each call. And presumably people who are asking for the minimum/maximum of a range shouldn't be doing that in the first place :-)
Wait a moment... actually, why do you think that and should be better than <= there?
Because it will not have to do the second and third comparison if the first 0 <= min(nums) fails. "Should" is of course strictly algorithmically speaking. With the data you used for timing, that early break will not come into action often. But generally, it is obvious that the test min(nums) <= max(nums) is unnecessary
For my timings with the same data, the 'and' approach was insignificantly faster. But, of course, the other thing I said, was nonsense :D x <= y <= z also stops evaluating after the first False sub equation.
But if 0 <= min(nums) fails then my version with <= in the middle will also stop right away and not do the rest.
22:54
Yup thats what I m saying. The question thne becomes if and id faster than <=
Ah darn, I missed your second paragraph there...
23:28
Just tested "0 <= a <= b <= 255" and "0 <= a and b <= 255" with "a, b = 1, 2". The "and"-version was about factor 1.6 faster. But in practice with "a" and "b" being expensive computations it's insignificant, as you say. I vaguely remember testing this before. Not sure why I ended up using "<=". Maybe because it's shorter and because "and" feels meaningless compared to "<=". Or because I just can never get enough of chained comparisons, I love them so much :-)

last day (15 days later) »