Why is string comparison so fast in python?

前端 未结 2 1442
死守一世寂寞
死守一世寂寞 2020-12-29 02:59

I became curious to understand the internals of how string comparison works in python when I was solving the following example algorithm problem:

Give

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 03:58

    This is both implementation-dependent and hardware-dependent. Without knowing your target machine and specific distribution, I couldn't say for sure. However, I strongly suspect that the underlying hardware, like most, has memory block instructions. Among other things, this can compare a pair of arbitrarily long strings (up to addressing limits) in parallel and pipelined fashion. For instance, it may compare 8-byte slices at one slice per clock cycle. This is a lot faster than fiddling with byte-level indices.

提交回复
热议问题