optimize your java code by tools
DESCRIPTION
Optimize Your Java Code By Tools. NuPoint Technology Inc. Manager of R&D Dept. Lin, Weicheng. Agenda. Why to Optimize Your Code? Introduction Optimize by OptimizeIt Conclusions. Why to Optimize Your Code?. Why coding? - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/1.jpg)
1
NUPOINT TECHNOLOGY INC.MANAGER OF R&D DEPT.
LIN, WEICHENG
Optimize Your Java Code By Tools
![Page 2: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/2.jpg)
2
Agenda
Why to Optimize Your Code?
Introduction
Optimize by OptimizeIt
Conclusions
![Page 3: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/3.jpg)
3
Why to Optimize Your Code?
Why coding?
Why do your program need more execution time or developing time than that of others’?
Do you agree that Programming is an ART?
![Page 4: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/4.jpg)
4
Introduction
How to write a good program? Suitable language
C++, Java, Perl, Scheme, … Good algorithm
You need to learn Algorithm, OS, … Good implementation
You need not only a good brain but a good tool
Bad implementation Allocate too much memory Instantiate too many objects Bad programmatic logic …
![Page 5: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/5.jpg)
5
Introduction (Cont.)
There are several tools for optimization use: OptimizeIt, …
Functions of Optimization Tools Profiling Code Coverage Checking Thread Debugging Progress Tracking Request Analyzing
![Page 6: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/6.jpg)
6
Optimize by OptimizeIt
The goal of the optimization
A quick introduction to the application
Application architecture
Integrating JBuilder and OptimizeIt
Optimizing your first project
![Page 7: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/7.jpg)
7
The Goal of the Optimization
Using JBuilder and OptimizeIt to solve performance problems with an email agent A user with 4000 Messages per day
Before the performance tuning Several minutes
After the performance tuning < 10 seconds
Improvement Less execution time Less required memory
![Page 8: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/8.jpg)
8
A Quick Introduction to the Application
1. Spam filteringIt deletes spam from a user's mailbox using a variety of spam-filtering rules
2. Forwarding of desirable email messages to a pagerA user can control which email messages should be forwarded from their inbox to their pager
3. Mail server-independenceThe Email Agent works with any email server that supports the POP-3 or IMAP standards
4. Platform-independenceThe Email Agent can run on any computer platform that supports Java, making it very flexible to install
![Page 9: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/9.jpg)
9
Application Scenario
Scenario Currently, only a user, with 4,000 messages About 1,000 spam messages Running every five minutes In the need of handling more users
![Page 10: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/10.jpg)
10
Application Architecture
![Page 11: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/11.jpg)
11
Basic Algorithm of the Email Agent
1. Application startup. 2. Initialize logger. 3. Read mail server connection properties. 4. Read properties for sending a summary report to the user. 5. Initialize internal lists from configuration files. 6. Create and populate the EmailAgent. 7. Connect to the mailbox. 8. Remove spam from the current folder.
1. Get the message list from the inbox. 2. Determine the UIDs of these messages. 3. Get a list of messages that have already been examined already. 4. While only looking at new messages:
1. One at a time, review each message to determine whether or not it is spam. 1. If the message matches an ``allow'' file rule, let it pass through. 2. Otherwise, test the From, Reply-To, and Subject fields, as well as message attachments.
2. If a message matches a notification criteria, send a message to my pager.
9. Close the mailbox and store. 2. Send me summary information about what actions were taken.
Be care of your for-loop!Be care of your for-loop!
![Page 12: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/12.jpg)
12
Basic Configuration
Configuring the application allow.from allow.subject allow.contents reject.from reject.subject reject.contents
![Page 13: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/13.jpg)
13
Integrating JBuilder and OptimizeIt
Installation
![Page 14: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/14.jpg)
14
Integrating JBuilder and OptimizeIt (Cont.)
![Page 15: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/15.jpg)
15
Optimizing Your First Project
![Page 16: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/16.jpg)
16
Finding the Performance Problems
Java Performance Tuning'' by Jack Shirazi
1. Measure the performance of the application, using tools, and/or by instrumenting the code.
2. Identify the location of any bottlenecks. 3. Think of a hypothesis for the cause of the bottleneck. 4. Consider any factors that might refute your hypothesis. 5. Create a test to isolate the factor identified by the
hypothesis. 6. Test the hypothesis. 7. Alter the application to reduce the bottleneck. 8. Test that the alteration improves the performance. 9. Repeat from step 1.
![Page 17: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/17.jpg)
17
Finding the Performance Problems (Cont.)
Simplified version of the process 1. Instrument your code and find the major bottlenecks,
using the best tools available 2. Fix the bottlenecks that are causing the biggest
problems, don't worry about the others. If you can't fix it, avoid it
3. If performance is now acceptable, celebrate. If not, repeat from step 1
![Page 18: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/18.jpg)
18
Finding the Performance Problems (Cont.)
Fix the bottlenecks that are causing the biggest problems and don't worry about the others Simplified analysis example
examineContents() - 70% extractContents() - 20% parseAttachment() - 10%
![Page 19: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/19.jpg)
19
The First Run: An Example of How to Use Avoidance Therapy
![Page 20: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/20.jpg)
20
![Page 21: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/21.jpg)
21
Drill Down Your Programmatic Flow
Using Contents needs a lotof time
1. Put this line in the last2. It will be run when needed
![Page 22: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/22.jpg)
22
![Page 23: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/23.jpg)
23
The Second Run
Built in JDK Built in JDK
![Page 24: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/24.jpg)
24
The Third Run: Problems with Regular Expressions
aMatcher.find() wastes too much time
aMatcher.find() wastes too much time
indexOf is faster
indexOf is faster
Change the order!
Change the order!
![Page 25: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/25.jpg)
25
9,956 ms9,956 ms
aMatcher.find() is still slow, we use aMatcher.matches()aMatcher.find() is still slow, we use aMatcher.matches()
![Page 26: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/26.jpg)
26
![Page 27: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/27.jpg)
27
9,548 ms9,548 ms
![Page 28: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/28.jpg)
28
![Page 29: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/29.jpg)
29
Getting Harder Now
9,548 ms9,548 ms
![Page 30: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/30.jpg)
30
Getting Harder Now (Cont.)
9,353 ms9,353 ms
![Page 31: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/31.jpg)
31
Conclusions
Reducing the application runtime from over 52 seconds down to about 9.4 seconds
You really need a optimization tool, don’t you?
Now, do you agree that Programming is an ART?
![Page 32: Optimize Your Java Code By Tools](https://reader034.vdocuments.mx/reader034/viewer/2022052603/56812b24550346895d8f2137/html5/thumbnails/32.jpg)
32
Thanks!