Converting ints to Strings
5.3.2 Converting ints to Strings
While the previous append version is suitable to use for int s by overloading, it is much more efficient to create another version specifically for int s. This is because int arithmetic is optimal and considerably faster than the long arithmetic being used. Although earlier versions of the JDK before JDK 1.1.6 used an inefficient conversion procedure for int s, from 1.1.6 onward Sun targeted the conversion for radix 10 integers only and speeded it up by an order of magnitude. To better this already optimized performance, you need every optimization available. There are three changes you can make to the long conversion algorithm already presented. First, you can change everything to use int s. This gives a significant speedup more than a third faster than the long conversion. Second, you can inline the magnitude method. And finally, you can unroll the loop that handles the digit-by-digit conversion. In this case, the loop can be completely unrolled since there are at most 10 digits in an int . The resulting method is a little long-winded: public static void appendStringBuffer s, int i { if i 0 { if i == Integer.MIN_VALUE { cannot make this positive due to integer overflow s.append-2147483648; return this; } s.append-; i = -i; } int mag; int c; if i 10 one digit s.appendcharForDigit[i]; else if i 100 two digits s.appendcharForDigit[i10] .appendcharForDigit[i10]; else if i 1000 three digits s.appendcharForDigit[i100] .appendcharForDigit[c=i10010] .appendcharForDigit[c10]; else if i 10000 four digits s.appendcharForDigit[i1000] .appendcharForDigit[c=i1000100] .appendcharForDigit[c=10010] .appendcharForDigit[c10]; else if i 100000 five digits s.appendcharForDigit[i10000] .appendcharForDigit[c=i100001000] .appendcharForDigit[c=1000100] .appendcharForDigit[c=10010] - 104 - .appendcharForDigit[c10]; else if i 1000000 six digits ... Im sure you get the idea else if i 10000000 seven digits ... so just keep doing the same, but more else if i 100000000 eight digits ... because my editor doesnt like wasting all this space else if i 1000000000 nine digits ... on unnecessary repetitions else { ten digits s.appendcharForDigit[i1000000000]; s.appendcharForDigit[c=i1000000000100000000]; s.appendcharForDigit[c=10000000010000000]; s.appendcharForDigit[c=100000001000000]; s.appendcharForDigit[c=1000000100000]; s.appendcharForDigit[c=10000010000]; s.appendcharForDigit[c=100001000]; s.appendcharForDigit[c=1000100]; s.appendcharForDigit[c=10010]; s.appendcharForDigit[c10]; } } If you compare this implementation to executing StringBuffer.appendint , the algorithm listed here runs in less time for all except the latest VM, and creates two fewer objects [3] see Table 5-2 . This is faster than the JDK optimized version, has a smaller impact on garbage creation, and has all the other advantages previously listed for the long conversion i.e., it is easily generalized for other radix values, digits are iterated in order so you can write to a stream, and it is easier to alter for formatting without using temporary objects. Note that the long conversion method can also be improved using two of the three techniques we used for the int conversion method: inlining the magnitude method and unrolling the loop. [3] If the StringBuffer.appendint used the algorithm shown here, it would be faster for all JDK versions measured in this chapter, since the characters could be added directly to the char buffer without going through the StringBuffer.appendchar method. Table 5-2, Time Taken to Append an int to a StringBuffer VM 1.2 1.3 HotSpot 1.0 1.1.6 JDK int conversion 100 61 89 148 Optimized int conversion 84 60 81 1115.3.3 Converting bytes, shorts, chars, and booleans to Strings
Parts
» OReilly.Java.performance tuning
» The Tuning Game System Limitations and What to Tune
» A Tuning Strategy Introduction
» Threading to Appear Quicker Streaming to Appear Quicker
» User Agreements Starting to Tune
» Setting Benchmarks Starting to Tune
» The Benchmark Harness Starting to Tune
» Taking Measurements Starting to Tune
» What to Measure Introduction
» Dont Tune What You Dont Need to Tune
» Measurements and Timings Profiling Tools
» Garbage Collection Profiling Tools
» Profiling Methodology Method Calls
» Java 2 cpu=samples Profile Output
» HotSpot and 1.3 -Xprof Profile Output
» JDK 1.1.x -prof and Java 2 cpu=old Profile Output
» Object-Creation Profiling Profiling Tools
» Monitoring Gross Memory Usage
» Replacing Sockets ClientServer Communications
» Performance Checklist Profiling Tools
» Garbage Collection Underlying JDK Improvements
» Replacing JDK Classes Underlying JDK Improvements
» VM Speed Variations VMs with JIT Compilers
» Other VM Optimizations Faster VMs
» Inline calls Remove dynamic type checks Unroll loops Code motion
» Literal constants are folded String concatenation is sometimes folded Constant fields are inlined
» Optimizations Performed When Using the -O Option
» Performance Effects From Runtime Options
» Compile to Native Machine Code
» Native Method Calls Underlying JDK Improvements
» Uncompressed ZIPJAR Files Underlying JDK Improvements
» Performance Checklist Underlying JDK Improvements
» Object-Creation Statistics Object Creation
» Pool Management Object Reuse
» Reusable Parameters Object Reuse
» String canonicalization Changeable objects
» Weak references Canonicalizing Objects
» Avoiding Garbage Collection Object Creation
» Preallocating Objects Lazy Initialization
» Performance Checklist Object Creation
» The Performance Effects of Strings
» Compile-Time Versus Runtime Resolution of Strings
» Converting bytes, shorts, chars, and booleans to Strings Converting floats to Strings
» Converting doubles to Strings
» Converting Objects to Strings
» Word-Counting Example Strings Versus char Arrays
» Line Filter Example HotSpot 1.0
» String Comparisons and Searches
» Sorting Internationalized Strings Strings
» The Cost of try-catch Blocks Without an Exception
» The Cost of try-catch Blocks with an Exception
» Using Exceptions Without the Stack Trace Overhead Conditional Error Checking
» no JIT 1.3 Variables Strings
» Method Parameters Performance Checklist
» Exception-Terminated Loops Loops and Switches
» no JIT 1.3 Loops and Switches
» Recursion Loops and Switches
» no HotSpot 1.0 2nd Loops and Switches
» Recursion and Stacks Loops and Switches
» Performance Checklist Loops and Switches
» Replacing System.out IO, Logging, and Console Output
» Logging From Raw IO to Smokin IO
» no JIT HotSpot 1.0 no JIT HotSpot 1.0 Serialization
» no IO, Logging, and Console Output
» Clustering Objects and Counting IO Operations
» Compression IO, Logging, and Console Output
» Performance Checklist IO, Logging, and Console Output
» Avoiding Unnecessary Sorting Overhead
» An Efficient Sorting Framework
» no HotSpot Better Than Onlogn Sorting
» User-Interface Thread and Other Threads
» Desynchronization and Synchronized Wrappers
» Avoiding Serialized Execution HotSpot 1.0
» no JIT no JIT HotSpot 1.0 Timing Multithreaded Tests
» Atomic Access and Assignment
» Free Load Balancing from TCPIP
» Load-Balancing Classes Load Balancing
» A Load-Balancing Example Load Balancing
» Threaded Problem-Solving Strategies Threading
» Collections Appropriate Data Structures and Algorithms
» Java 2 Collections Appropriate Data Structures and Algorithms
» Hashtables and HashMaps Appropriate Data Structures and Algorithms
» Cached Access Appropriate Data Structures and Algorithms
» Caching Example I Appropriate Data Structures and Algorithms
» Caching Example II Appropriate Data Structures and Algorithms
» Finding the Index for Partially Matched Strings
» Search Trees Appropriate Data Structures and Algorithms
» Comparing Communication Layers Distributed Computing
» Batching I Application Partitioning
» Compression Caching Low-Level Communication Optimizations
» Transfer Batching Low-Level Communication Optimizations
» Batching II Distributed Garbage Collection
» Performance Checklist Distributed Computing
» When Not to Optimize Tuning Class Libraries and Beans
» Scaling Design and Architecture
» Distributed Applications Design and Architecture
» Object Design Design and Architecture
» Use simulations and benchmarks Consider the total work done and the design overhead
» Tuning After Deployment When to Optimize
» User Interface Usability Training Server Downtime
» Performance Checklist When to Optimize
» Clustering Files Cached Filesystems RAM Disks, tmpfs, cachefs
» Disk Fragmentation Disk Sweet Spots
» RAM Underlying Operating System and Network Improvements
» Network Bottlenecks Network IO
» Performance Checklist Underlying Operating System and Network Improvements
Show more