Available CategoriesAdobeMacromediaProgrammingSQLServer AdministrationNetworkingMicrosoft ProductsMac OSLinux systemsMobile devicesXMLCertificationMiscAvailable Tutorials.NET Framework Essentials.NET Programming securityC# In A Nutshell TutorialProgramming C.SharpMastering Visual Studio .NETASP.NETWeb Solutions based on ASP.NET and ADO.NETJava data objectsJava extreme programmingJava performance tuningJava development on pda's. Building applications for pocket pc and palm devicesJavaScript and DHTMLLearning UMLUMLLearning XMLCocoaProgramming CppPerl objects, references and modulesPerl tutorialPython tutorialPython. Text processingPocket pc network programmingPHP & MySQL. Building web database applicationsPHP & MySQL. Programming for beginnersPHP, MySQL and Apache in 24 hoursSoftware architecture in practiceSoftware engineering and computer gamesBuilding Solutions With the Microsoft .NET Compact FrameworkProgramming Microsoft Visual C# 2005ActionscriptMastering Delphi 7Ado.netPractical mod_perlPerl for bioinformaticsWeb ServicesPrinciples of Secure CodingC/C++ Secure ProgrammingASP.NET AJAXVisual C#Borland C++ Builder 6 Developer's Guide |
5.1 The Performance Effects of Strings
The disadvantages of the String implementation are:
The advantages of Strings can be summed up as ease of use, internationalization support, and compatibility to existing interfaces. Most methods expect a String object rather than a char array, and String objects are returned by many methods. The disadvantage of Strings boils down to inflexibility. With extra work, most things you can do with String objects can be done faster and with less intermediate object-creation overhead by using your own set of char array manipulation methods. For most performance tuning, you pinpoint a bottleneck and make localized changes to objects and methods that speed up that bottleneck. But String tuning often involves converting to char arrays, whereas you rarely come across public methods or interfaces that deal in char arrays. This makes it difficult to switch between Strings and char arrays in any localized way. The consequences are that you either have to switch back and forth between Strings and char arrays, or you have to make extensive modifications that can reach across many application boundaries. I have no easy solution for this problem. String tuning can get messy. Sun recognizes that Strings are not the optimal solution in many cases and has added a CharSequence interface in JDK 1.4 that String and other classes implement. New methods have been added that operate on CharSequence objects rather than requiring Strings. For example, the regular expression classes accept CharSequence objects. This doesn't necessarily help your particular bottleneck, and CharSequences still access the char elements through a charAt( ) method, but it does at least increase the options available for optimizing applications. It is difficult to handle String internationalization capabilities using raw char arrays. But in many cases, internationalized Strings form a specific subset of String usage in an application, mainly in the user interface, and that subset of Strings rarely causes bottlenecks. You should differentiate between Strings that need internationalization and those that are simply processing characters, independent of language. These latter Strings can be replaced for tuning with char arrays.[2] Internationalization-dependent Strings are more difficult to tune, and I provide some examples of tuning these later in the chapter. Note also that internationalized Strings can be treated as char arrays for some types of processing without any problems; see Section 5.4.2 later in this chapter.
|