Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is integer comparison faster then string comparison?

Tags:

c++

string

stl

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!

like image 792
MisterQuestion Avatar asked Feb 05 '11 00:02

MisterQuestion


People also ask

Is integer comparison faster than string comparison?

2, but in general int comparison is way faster than string.

How do you compare strings and integers?

If you want to compare their string values, then you should convert the integer to string before comparing (i.e. using String. valueOf() method). If you compare as integer values, then 5 is less than "123". If you compare as string values, then 5 is greater than "123".

What is the time complexity of comparing two strings?

Time Complexity: O(min(n,m)) where n and m are the length of the strings. Auxiliary Space: O(max(n,m)) where n and m are the length of the strings.

Do comparison operators work on strings?

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.


4 Answers

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.

like image 73
Sebastian Paaske Tørholm Avatar answered Oct 13 '22 01:10

Sebastian Paaske Tørholm


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.

like image 36
Ben Voigt Avatar answered Oct 12 '22 23:10

Ben Voigt


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

like image 20
Martin Jespersen Avatar answered Oct 13 '22 01:10

Martin Jespersen


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).

like image 28
Nawaz Avatar answered Oct 13 '22 01:10

Nawaz