Optimizing the performance of decimal string to hexadecimal in Java can start by reducing object creation, avoiding regular expressions, and using efficient data structures.
The following are the specific optimization solutions:
1. Avoid string segmentation and directly traverse character arrays
Original method(Frequently create substrings):
String twoChars = (i, i + 2); int decimalValue = (twoChars);
Optimization method(Directly parse character array):
int value = ((i) - '0') * 10 + ((i + 1) - '0');
2. Use a StringBuilder with preallocated capacity
Before optimization(Default capacity, may be expanded multiple times):
StringBuilder hexBuilder = new StringBuilder();
After optimization(Pre-allocated capacity, reduce the number of expansions):
StringBuilder hexBuilder = new StringBuilder(() / 2);
3. Manually implement the number to hexadecimal system to avoid
Original method(Use formatting, the overhead is high):
String hexPart = ("%02X", decimalValue);
Optimization method(Manual conversion, higher performance):
char[] hexChars = new char[2]; hexChars[0] = toHexChar(decimalValue >>> 4); // High 4 digitshexChars[1] = toHexChar(decimalValue & 0xF); // Lower 4 digits(hexChars); // Helper method: convert numbers from 0-15 into hexadecimal charactersprivate static char toHexChar(int num) { return (char) (num < 10 ? num + '0' : num - 10 + 'A'); }
4. Batch processing of characters to reduce method call overhead
public static String decimalToHex(String decimalStr) { int length = (); if (length == 0) return ""; char[] decimalChars = (); char[] hexChars = new char[(length + 1) / 2 * 2]; // Result array int hexIndex = 0; for (int i = 0; i < length; i += 2) { int d1 = decimalChars[i] - '0'; int value = d1; // Check if there is a second character if (i + 1 < length) { int d2 = decimalChars[i + 1] - '0'; value = value * 10 + d2; } // Convert to hexadecimal characters hexChars[hexIndex++] = toHexChar(value >>> 4); hexChars[hexIndex++] = toHexChar(value & 0xF); } return new String(hexChars, 0, hexIndex); }
5. Optimization of handling odd-length strings
For inputs with odd lengths, the last character is processed separately:
// Process the last character (if the length is odd)if (length % 2 != 0) { int d = decimalChars[length - 1] - '0'; hexChars[hexIndex++] = '0'; hexChars[hexIndex++] = toHexChar(d); }
6. Avoid regular expressions (if not required)
Before optimization(Use regular filtering of non-numeric characters):
decimalStr = ("[^0-9]", "");
After optimization(Manual filtering, higher performance):
// Manually filter non-numeric charactersint filteredLength = 0; for (int i = 0; i < length; i++) { char c = (i); if (c >= '0' && c <= '9') { decimalChars[filteredLength++] = c; } }
The optimized complete code
public static String decimalToHex(String decimalStr) { if (decimalStr == null || ()) { return ""; } char[] decimalChars = (); int length = ; // Preallocate result array (large enough) char[] hexChars = new char[length * 2]; int hexIndex = 0; // Process every two characters for (int i = 0; i < length - 1; i += 2) { int d1 = decimalChars[i] - '0'; int d2 = decimalChars[i + 1] - '0'; int value = d1 * 10 + d2; hexChars[hexIndex++] = toHexChar(value >>> 4); hexChars[hexIndex++] = toHexChar(value & 0xF); } // Process the last character (if the length is odd) if (length % 2 != 0) { int d = decimalChars[length - 1] - '0'; hexChars[hexIndex++] = '0'; hexChars[hexIndex++] = toHexChar(d); } return new String(hexChars, 0, hexIndex); } private static char toHexChar(int num) { return (char) (num < 10 ? num + '0' : num - 10 + 'A'); }
Performance comparison test
Benchmarking 1 million conversions (Input:"255015"
):
method | Time-consuming (milliseconds) | Memory usage (MB) |
---|---|---|
Original method | ~250 | ~120 |
Optimization method | ~80 | ~40 |
Performance optimization summary
1) Reduce object creation:avoidsubstring
、and
。
2) Use basic data types: Direct operationchar[]
array, not string.
3) Preallocated memory:forStringBuilder
Or character array preallocates sufficient capacity.
4) Reduce method calls: Simple method inline (such as character conversion).
5) Manually parse characters: Directly calculate the values, rather than relying on library methods.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.