Home > Back-end >  String equals comparison method and the type int the==comparison method, efficiency, fast, low perfo
String equals comparison method and the type int the==comparison method, efficiency, fast, low perfo

Time:09-25

Morning when doing development thought of a problem, the needs of the scene is 100 item in the list (item included in the download task id, download url task variables) such as elements, each item is more real-time refresh, download progress download callback interface progress update method of parameter in three: download task id of type int, String types of links to download the task, there is a download percentage of type int, existing logic is in progress update method according to the type String url search to the item, to refresh the list again, I also tried another way, is to find more ing type task id to the item, to refresh,
That I'm confused, the two different data types different comparison method, what kind of the high efficiency of some?

CodePudding user response:

You can test yourself, the comparison of type int is faster than the comparison of type String as a lost

CodePudding user response:

Int definitely faster than equal, equal way to String is after the hash is hash value, is essentially a CPU type operation, if the String is particularly big, calculation process is slow, a String of 1 m should be around 10 ms, but int==method, is the memory between the size of the data comparison, is a simple calculation (add, subtract, multiply or divide,

CodePudding user response:

==
Assuming that==operating expenses, time of 10,
You go to see inside the equals method again, he had more statements, finally must include at least one==, so spending time equals method more than 10,

Contains the relationship

CodePudding user response:

refer to the second floor cch5487614 response:
int definitely faster than equal, equal way to String is after the hash is hash value, is essentially a CPU type operation, if the String is particularly big, calculation process is slow, a String of 1 m should be around 10 ms, but int==method, is the memory between the size of the data comparison, is a simple calculation (add, subtract, multiply or divide


String length equals should be the first to compare, and then from a a comparison are the same

CodePudding user response:

But Int==sure is than String equals faster, because String. Equals is at least one time length (Int)==

CodePudding user response:

Piss me off, talent!
 public static void main (String [] args) throws the Exception {
Int a=5;
Int b=6;
Long startTime=System. CurrentTimeMillis ();
for (int i=0; i <1000 _0000; I++) {
If (a==b) {

}
}
Long useTime=System. CurrentTimeMillis () - startTime;
System. The out. Println (" useTime="+ useTime);
}

After the test, the running time is 2 ~ 3 milliseconds, then the variable type change, as follows:
 String a="5"; 
String b="6";

Other code unchanged, and running time is 3 ~ 5 ms, see, is to use==to compare a String, it is slower than the==comparison int, of course the==to compare a String comparison is actually memory address (int), but the memory address is generally a larger int value, so the CPU is slower, if you use the equals to compare a String, the efficiency must be slower, you can like me to write a Demo test running time to know,

CodePudding user response:

If I solved your problem, answer to trouble and stick to the point integral, thank ^_^