Assignment Versus Equality
bc at freeuk.com
Wed Jun 29 06:10:34 EDT 2016
On 29/06/2016 10:56, Lawrence D’Oliveiro wrote:
> On Wednesday, June 29, 2016 at 9:49:23 PM UTC+12, BartC wrote:
>> Even if Python has extremely efficient string handling, we know that
>> low-level string ops normally take longer than low-level integer ops.
> Maybe part of the general principle that, on modern machines, memory is cheap, but accessing memory is expensive?
No, it's just fewer instructions. If you do the equivalent of a==b where
both are integers, it might be a couple of instructions in native code.
If both are strings, even of one character each (say the code is
choosing to compare "A" with "B" instead of ord("A") with ord("B"), then
it's a /lot/ more than two instructions.
(With Python there's the side-issue of actually getting the integer
values. Having to call ord() doesn't help the case for using integers.)
More information about the Python-list