Efficient version number comparison in Java

Source: Internet
Author: User


We all know that the client or device side related to the version number of the place will inevitably involve the version upgrade problem, this time we need to compare the size of the version number, String type version number is how to compare it? Traditionally we will use CompareTo or string to double the comparison, so there will be a bug, today I would like to talk about two ways of comparison: split according to "." To split, and then compare the size of each split out of the string, the following code: (in addition, I use the version number does not contain letters, so do not consider the case contains letters, this can make corresponding changes)

public static int Compare (String v1,string v2) {string [] xx=v1.split ("\ \"); String [] yy=v2.split ("\ \"); int A=0;try {for (int x=0,y=0;x<xx.length| | y<yy.length;x++,y++) {int left= (x<xx.length)? Integer.parseint (Xx[x]): 0;int right= (y<yy.length)? Integer.parseint (Yy[y]): 0;if (left>right) {a=1;} else if (left==right) {continue;} Else{a=-1;} a=0;}} catch (Exception e) {//Todo:handle exception}return A;}

But from a performance point of view, this approach is not the most perfect, we have a similar to the bottom of the way, you can better save performance, running speed has improved, nonsense said, look at the code:

        public static  Int compare2 (string v1,string v2) {int i=0,j=0,x=0,y=0;int v1len=v1.length ();int  V2len=v2.length (); Char c;do {while (I<v1len) {//calculates the number C=v1.charat before the point in V1 (i++); if (c>= ' 0 '   && c<= ' 9 ') {x=x*10+ (c ' 0 ');//c-' 0 ' represents the ASCLL difference between the two}else if (c== '. ') {break;//End}else{//Invalid character}}while (J<v2len) {//Calculates the number V2 (j + +) before the point in C=v2.charat; if (c>= ' 0 '  &&  c<= ' 9 ') {y=y*10+ (c ' 0 ');} Else if (c== '. ') {break;//End}else{//Invalid character}}if (x<y) {return -1;} Else if (x>y) {return 1;} Else{x=0;y=0;continue;}}  while  ((i<v1len)  | |   (J<v2len)); return 0;} 
public static void Main (string[] args) {System.out.println ("Compare performance start >>>>>>>>>>>> > "); String v1= "0.10.20.1"; String v2= "0.001.02.3"; Long Starttime=system.currenttimemillis (); for (int i=0;i<1000000;i++) {Compare (V1, v2);} Long Endtime=system.currenttimemillis (); SYSTEM.OUT.PRINTLN ("split mode comparison 1 million times" + (Endtime-starttime)); Long Starttime1=system.currenttimemillis (); for (int i=0;i<1000000;i++) {compare2 (v1, v2);} Long Endtime1=system.currenttimemillis (); System.out.println ("character char method compares 1 million times" + (ENDTIME1-STARTTIME1));}

The results of the test are as follows:

Compare performance Start >>>>>>>>>>>>>

Split mode comparison 1 million times 538

Character Char mode comparison 1 million times 28


As a result, the first time to write a blog, the wrong place also please correct


This article is from the "Java into God's Road" blog, please be sure to keep this source http://yao012.blog.51cto.com/12765256/1915026

Efficient version number comparison in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.