开发者

Why is integer comparison faster then string comparison?

I found comments about avoiding strings for the comparison of values (especially in loops) in a few books because string comparison is a lot slower (using std::string). But why exactly is that?

Is it because integer units in the cpu are working faster?

Strings should be开发者_开发百科 in byte I guess, so wouldn't a byte comparison do the job equally?

Thanks!


With an integer, there exist instructions on the machine level which can perform a comparison in one cycle.

A string, however, consists of a lot of characters. In order to compare strings, you, in the worst case, have to look at every character of the strings.

In fact, when you compare strings, you're most likely using an integer comparison for each character in the string. You can probably see how this quickly can turn into a lot of comparisons as compared to comparing two integers.

Example: If you want to compare 1073741822 with 1073741823.

  • String comparison: You have to compare each of the digits one by one. This is 10 comparisons, since the integers only differ by the last digit.
  • Integer comparison: You can do this in one comparison, saving 9 comparisons as compared to the String comparison.

This is a bit simplified, naturally, but hopefully gets the point across.


The issue is that a string comparison isn't just a single comparison, it's a whole sequence of them in a loop. What do you expect to happen if you compare two strings that are each 10001 characters long and the first 9000 are the same?

BTW SSE makes string compare a LOT faster than one-character-at-a-time, but it can never reach the speed of an integer compare.


The compiler can optimize integer comparisons to happen directly inside the cpu registers.


Vaguely speaking, string comparison requires n number of integer comparison where n is the size of string, whereas integer comparison is just one comparison if you think ratio-wise. It's a rough idea of what might be going when comparing strings!

So obviously string comparison would be a slower process, since it's n number of integer comparisons (approx).


Integers are usually 4-bytes.

Strings are anywhere between 1 and infinity* bytes.

*Hey, you get what I mean, don't you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜