Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is number comparison faster than string comparison?

Tags:

I heard that hashing (ie converting a string or object to a number) is used for strings and such because it is easier to compare numbers than strings. If true, what is the reason for this ?

like image 589
FirstName LastName Avatar asked May 12 '13 02:05

FirstName LastName


People also ask

Is it faster to compare strings or numbers?

Comparing primitive numbers is definitely faster than comparing strings because it's just one computer instruction while comparing strings in Java is a method.

Which is faster equals or ==?

Specifically with regard to strings, yes, == is slightly faster than equals , because the first thing the String.

Can you compare number with string?

That is it. You can convert a numeric string into integer using Integer. parseInt(String) method, which returns an int type. And then comparison is same as 4 == 4 .

How fast is string comparison in Javascript?

String equality is slightly faster at 13.15ms vs BigInt at 16.57ms for 100k comparisons. Set operations are slower with String taking 18.02ms and BigInt at 29.64ms for 100k has operations.

What is the difference between string comparison and integer comparison?

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

How do you compare strings?

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.

Is the C string compare faster than the Java equivalent?

The C string compare is simpler / faster than the Java equivalent but it will contain some sort of loop and multiple instructions for each pass through the loop. Show activity on this post. Yes, but that has nothing to do with hashing.

Why is comparing two strings so hard?

Comparing two strings is very slow and expensive. Most algorithms require iterating through entire string and matching each character. For example let's say we want to compare 9 with 12 (false). For numeric comparison, let's assume the algorithm compares individual bit. 9 = 1001 12 = 1100


2 Answers

This is not necessarily the case, but probably the case most of the time.

Consider the following situation:

I want to compare the string "apples" vs. "oranges". If I only want to determine "apples" == "oranges", I need only compare the first character of each string: 'a' != 'o' => "apples" != "oranges". If I hash the string and then do the comparison, it is significantly slower as I have to parse both strings and feed them into a hashing algorithm before comparing the resultant integers.

If, however, I need to do this comparison many times, and perhaps I'm comparing "oranges" to "orangutans" a lot, then if I hash all the strings once and do the comparisons of integers many times, it will work out faster. This is the principle that a hash map is based on.

Note, however, that hashing a string is useful for direct equals comparisons, it cannot determine if strings are lexigraphically greater or less than each other, and so ordering strings is not possible via the hashing method. (This is why HashMap in Java is unordered).

like image 112
Barney Govan Avatar answered Sep 24 '22 05:09

Barney Govan


Comparing two numbers is magnitudes faster than comparing two strings (representing the same numbers). Comparing two numbers simply require comparing individual bits and could be done super fast using any of AND, XOR, 2's complements, etc.

Comparing two strings is very slow and expensive. Most algorithms require iterating through entire string and matching each character.

For example let's say we want to compare 9 with 12 (false). For numeric comparison, let's assume the algorithm compares individual bit. 9 = 1001 12 = 1100

Here, the worst case algorithm will compare 4 bits.

Now if we represent "9" and "12" as strings, they will be stored in memory as 16 bits each (Recall: Java uses UTF-16 to represent strings in memory) and have to be passed to a String comparison algorithm. In fact, Java's actual String comparison function is below:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}

As you can see, there's a lot more going around for String comparison.

like image 26
goblinjuice Avatar answered Sep 24 '22 05:09

goblinjuice