Questions tagged [comparison]

Questions about data comparison and efficient ways to accomplish it. Please avoid using this tag for generic (meta) comparison of two issues or concepts.

Questions about data comparison and efficient ways to accomplish it, e.g.: "Which is the fastest algorithm to compare two recordsets and decide which one has more palindromes?", or "What is the fastest way to compare two strings and say if they have more than Y characters in common?"

There are generally six basic "comparison operators": less than, <; less than or equal to, <=; equal to, ==; not equal to, !=; greater than or equal to, >=; and greater than, >.

8118 questions
514
votes
3 answers

Optimum way to compare strings in JavaScript?

I am trying to optimize a function which does binary search of strings in JavaScript. Binary search requires you to know whether the key is == the pivot or < the pivot. But this requires two string comparisons in JavaScript, unlike in C like…
HRJ
  • 17,079
  • 11
  • 56
  • 80
447
votes
8 answers

Comparing two strings, ignoring case in C#

Which of the following two is more efficient? (Or maybe is there a third option that's better still?) string val = "AStringValue"; if (val.Equals("astringvalue", StringComparison.InvariantCultureIgnoreCase)) OR if (val.ToLowerCase() ==…
pwmusic
  • 4,739
  • 4
  • 23
  • 14
446
votes
11 answers

How to compare dates in Java?

How do I compare dates in between in Java? Example: date1 is 22-02-2010 date2 is 07-04-2010 today date3 is 25-12-2010 date3 is always greater than date1 and date2 is always today. How do I verify if today's date is in between date1 and date 3?
ant
  • 22,634
  • 36
  • 132
  • 182
443
votes
12 answers

Image comparison - fast algorithm

I'm looking to create a base table of images and then compare any new images against that to determine if the new image is an exact (or close) duplicate of the base. For example: if you want to reduce storage of the same image 100's of times, you…
meade
  • 22,875
  • 12
  • 32
  • 36
434
votes
13 answers

Checking the equality of two slices

How can I check if two slices are equal, given that the operators == and != are not an option? package main import "fmt" func main() { s1 := []int{1, 2} s2 := []int{1, 2} fmt.Println(s1 == s2) } This does not compile with: invalid…
wei2912
  • 6,141
  • 2
  • 19
  • 20
434
votes
10 answers

Is there a "not equal" operator in Python?

How would you say "does not equal"? if hi == hi: print "hi" elif hi (does not equal) bye: print "no hi" Is there something similar to == that means "not equal"?
Aj Entity
  • 4,741
  • 4
  • 17
  • 13
399
votes
5 answers

Jackson Vs. Gson

After searching through some existing libraries for JSON, I have finally ended up with these two: Jackson Google GSon I am a bit partial towards GSON, but word on the net is that GSon suffers from a certain celestial performance issue (as of Sept…
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
388
votes
17 answers

What's the most efficient way to test if two ranges overlap?

Given two inclusive ranges [x1:x2] and [y1:y2], where x1 ≤ x2 and y1 ≤ y2, what is the most efficient way to test whether there is any overlap of the two ranges? A simple implementation is as follows: bool testOverlap(int x1, int x2, int y1, int y2)…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
365
votes
10 answers

How to check if a variable is not null?

I know that below are the two ways in JavaScript to check whether a variable is not null, but I’m confused which is the best practice to use. Should I do: if (myVar) {...} or if (myVar !== null) {...}
nickb
  • 9,140
  • 11
  • 39
  • 48
357
votes
8 answers

Check if two unordered lists are equal

I'm looking for an easy (and quick) way to determine if two unordered lists contain the same elements: For example: ['one', 'two', 'three'] == ['one', 'two', 'three'] : true ['one', 'two', 'three'] == ['one', 'three', 'two'] : true ['one', 'two',…
KingFish
  • 8,773
  • 12
  • 53
  • 81
352
votes
12 answers

What is the rationale for all comparisons returning false for IEEE754 NaN values?

Why do comparisons of NaN values behave differently from all other values? That is, all comparisons with the operators ==, <=, >=, <, > where one or both values is NaN returns false, contrary to the behaviour of all other values. I suppose this…
starblue
  • 55,348
  • 14
  • 97
  • 151
351
votes
29 answers

Comparing two dictionaries and checking how many (key, value) pairs are equal

I have two dictionaries, but for simplification, I will take these two: >>> x = dict(a=1, b=2) >>> y = dict(a=2, b=2) Now, I want to compare whether each key, value pair in x has the same corresponding value in y. So I wrote this: >>> for x_values,…
user225312
  • 126,773
  • 69
  • 172
  • 181
340
votes
10 answers

Switch statement for greater-than/less-than

so I want to use a switch statement like this: switch (scrollLeft) { case (<1000): //do stuff break; case (>1000 && <2000): //do stuff break; } Now I know that either of those statements (<1000) or (>1000 && <2000) won't work (for…
switz
  • 24,384
  • 25
  • 76
  • 101
337
votes
14 answers

Can't compare naive and aware datetime.now() <= challenge.datetime_end

I am trying to compare the current date and time with dates and times specified in models using comparison operators: if challenge.datetime_start <= datetime.now() <= challenge.datetime_end: The script errors out with: TypeError: can't compare…
sccrthlt
  • 3,974
  • 5
  • 20
  • 24
300
votes
6 answers

Python None comparison: should I use "is" or ==?

My editor warns me when I compare my_var == None, but no warning when I use my_var is None. I did a test in the Python shell and determined both are valid syntax, but my editor seems to be saying that my_var is None is preferred. Is this the…
Clay Wardell
  • 14,846
  • 13
  • 44
  • 65