Android Interview Question 2: Converting Integer to String

The integer is converted to a string, five algorithms are written, and performance analysis is performed:

1. Algorithm: directly find the length first, and then find the size of each bit from left to right

	private static class Int2String1 implements Int2String {
		public String int2String(int intValue) {
			if (intValue == 0) {
				return "0";
			}
			int length = 0;
			boolean sign = false;
			if (intValue < 0) {
				intValue = -intValue;
				sign = true;
				length++;
			}
			int k = 1;
			while (true) {
				if (intValue >= k) {
					length++;
					k = k * 10;
				} else {
					break;
				}
			}

			char[] holder = new char[length];
			int start = 0;
			if (sign) {
				holder[0] = '-';
				start = 1;
			}

			for (; start < length; ++start) {
				int count = (length - start - 1);
				int pow10 = 1;
				for (int i = 0; i < count; i++) {
					pow10 = pow10 * 10;
				}
				holder[start] = (char) ((intValue / Math.pow(10, count)) + '0');
				intValue = intValue % pow10;
			}
			return new String(holder);
		}
	}

 Print Time:

1the time is 12122, strValue = -100121312

 

2. Algorithm: Insert directly from right to left, or from low to high. last caret bit

	private static class Int2String2 implements Int2String {
		public String int2String(int intValue) {
			char[] str = new char[0];
			boolean sign = false;
			if (intValue == 0) {
				return "0";
			}
			if (intValue < 0) {
				sign = true;
				intValue = -intValue;
			}
			while (true) {
				int leftValue = intValue % 10;
				char additionalChar = (char) (leftValue + '0');
				char[] old = str;
				str = new char[str.length + 1];
				str[0] = additionalChar;
				for (int i = 1; i < str.length; ++i) {
					str[i] = old[i - 1];
				}
				intValue = intValue / 10;
				if (intValue == 0) {
					break;
				}
			}
			if (sign) {
				char[] old = str;
				str = new char[str.length + 1];
				str[0] = '-';
				for (int i = 1; i < str.length; ++i) {
					str[i] = old[i - 1];
				}
			}
			return new String(str);
		}
	}

 Print Time:

 

1the time is 3426, strValue = -100121312

 

3. Algorithm: Calculate the length first, and compare it with a multiple of 10 when calculating the length. Then operate the value of each bit from right to left.

	private static class Int2String3 implements Int2String {
		public String int2String(int intValue) {
			if (intValue == 0) {
				return "0";
			}
			int length = 0;
			boolean sign = false;
			if (intValue < 0) {
				intValue = -intValue;
				sign = true;
				length++;
			}
			int k = 1;
			while (true) {
				if (intValue >= k) {
					length++;
					k = k * 10;
				} else {
					break;
				}
			}
			char[] holder = new char[length];
			int start = 0;
			if (sign) {
				holder[0] = '-';
				start = 1;
			}
			for (int i = length - 1; i >= start; --i) {
				holder[i] = (char) ((intValue % 10) + '0');
				intValue = intValue / 10;
			}
			return new String(holder);
		}
	}

 Print Time:

 1the time is 834, strValue = -100121312

 

4. Calculate the length first, and then calculate the value of each bit from right to left. The length is calculated modulo by division.

	private static class Int2String4 implements Int2String {
		public String int2String(int intValue) {
			int length = 0;
			boolean sign = false;
			if (intValue < 0) {
				sign = true;
				intValue = -intValue;
			}
			int k = intValue;
			while (k >= 0) {
				length++;
				k = k / 10;
				if (k == 0) {
					break;
				}
			}
			char [] charArray;
			if (sign) {
				length = length + 1;
				charArray = new char[length];
				charArray[0] = '-';
			} else {
				charArray = new char[length];
			}
			int m = intValue;
			int index = length - 1;
			while (true) {
				int leftValue = m % 10;
				charArray[index] = (char) (leftValue + '0');
				index--;
				m = m / 10;
				if (m == 0) {
					break;
				}
			}
			return new String(charArray);
		}
	}

 Print Time:

1the time is 1102, strValue = -100121312

 

5. Instead of calculating the length, use a buffer. The final construction string is constructed directly using this buffer. Because the number of digits in decimal must be less than the number of digits in 8. The conversion of 32-bit int to octal is 11 bits, and it is inferred that the maximum number of digits in decimal is also 11 bits.

	private static class Int2String5 implements Int2String {
		public String int2String(int intValue) {
			int position = 10;
			char[] buf = new char[11];
			if (intValue < 0) {
				intValue = -intValue;
				buf[0] = '-';
			}
			while (true) {
				int leftValue = intValue % 10;
				buf[position] = (char) (leftValue + '0');
				intValue = intValue / 10;
				if (intValue == 0) {
					break;
				} else {
					position--;
				}
			}
			return new String(buf, position, buf.length - position);
		}
	}

Print Time:

 

1the time is 742, strValue = 100121312

 

6. Use the algorithm that comes with the system

	private static class Int2String6 implements Int2String {
		public String int2String(int intValue) {
			return String.valueOf(intValue);
		}
	}

 

Print Time:

 

1the time is 764, strValue = -100121312

 

It can be seen that the algorithm provided by the java SDK is not the optimal algorithm. Algorithms that use buffers to avoid calculating lengths are faster. When the system memory requirements are not high, a public 11-bit buffer can be directly provided to do the conversion, which may be faster than the algorithm that comes with the java system.

 

So the following algorithm is obtained

	public static char[] buf = new char[11];
	private static class Int2String7 implements Int2String {
		public String int2String(int intValue) {
			int position = 10;
			if (intValue < 0) {
				intValue = -intValue;
				buf[0] = '-';
			}
			while (true) {
				int leftValue = intValue % 10;
				buf[position] = (char) (leftValue + '0');
				intValue = intValue / 10;
				if (intValue == 0) {
					break;
				} else {
					position--;
				}
			}
			return new String(buf, position, buf.length - position);
		}
	}

 Print:

 

1the time is 636, strValue = 100121312

 

However, this algorithm has a big problem, that is, the synchronization problem in a multi-threaded environment, so the synchronization problem is solved, so there is algorithm 7.

7. Solve the synchronization problem on the basis of 6:

	public static char[] buf = new char[11];
	private static class Int2String7 implements Int2String {
		public synchronized String int2String(int intValue) {
			int position = 10;
			if (intValue < 0) {
				intValue = -intValue;
				buf[0] = '-';
			}
			while (true) {
				int leftValue = intValue % 10;
				buf[position] = (char) (leftValue + '0');
				intValue = (intValue >>1)/5;
				if (intValue == 0) {
					break;
				} else {
					position--;
				}
			}
			return new String(buf, position, buf.length - position);
		}
	}

Print:

 

1the time is 616, strValue = 100121312

 

Summarize:

Transforming into strings and reducing the number of loops can significantly improve the operation speed. Multiplication is more efficient than division. Try to use multiplication instead of division.

 

If you see something, then download an APl software to support bloggers! It can also solve the trouble that you have too many passwords to remember.

Source code download link:

http://a.app.qq.com/o/simple.jsp?pkgname=com.wa505.kf.epassword

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326245523&siteId=291194637