On Mon, Mar 14, 2016 at 6:39 AM, BartC <b...@freeuk.com> wrote: > I used it in my benchmark to replace the if-else chain checking three lots > of ranges: > > switch(c) > if case(ord("A"),ord("B"),ord("C"),ord("D"),ord("E"),ord("F"), > ord("G"),ord("H"),ord("I"),ord("J"),ord("K"),ord("L"), > ord("M"),ord("N"),ord("O"),ord("P"),ord("Q"),ord("R"), > ord("S"),ord("T"),ord("U"),ord("V"),ord("W"),ord("X"), > ord("Y"),ord("Z")): > upper+=1 > elif case(ord("a"),ord("b"),ord("c"),ord("d"),ord("e"),ord("f"), > ord("g"),ord("h"),ord("i"),ord("j"),ord("k"),ord("l"), > ord("m"),ord("n"),ord("o"),ord("p"),ord("q"),ord("r"), > ord("s"),ord("t"),ord("u"),ord("v"),ord("w"),ord("x"), > ord("y"),ord("z")): > lower+=1 > elif case(ord("0"),ord("1"),ord("2"),ord("3"),ord("4"),ord("5"), > ord("6"),ord("7"),ord("8"),ord("9")): > digits+=1 > else: > other+=1 > > It worked, but took 110 seconds; 80 seconds without the ord's and comparing > strings (but I still think it's perverse that integer ops are slower than > string ops). > > But 110 or 80 seconds, the original Python was 3.6 seconds. (Probably, > someone could tweak it to work with ranges, but this is extra programmer > effort that you say is too valuable to waste on such matters.)
This is not comparing ranges, though. This is comparing against individual values. To talk about comparing ranges, I would expect the code to look something like this: switch(c) if case("A", "Z"): upper += 1 elif case("a", "z"): lower += 1 elif case("0", "9"): digits += 1 else: other += 1 THIS is comparing ranges. The underlying comparisons must be inequalities, not equalities. I absolutely *do not care* about performance until the code looks good - at least reasonably good. (By the way, your switch/case pair is non-reentrant. Plus it uses a class in a weird way. Why not, if you're working like this, just have two functions and a module-global? Just as non-reentrant, much cleaner code.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list