Solved

byte comparison and see efficiency of each program

Posted on 2014-10-11
7
145 Views
Last Modified: 2014-11-03
public String stringTimes(String str, int n) {
   StringBuilder sb = new StringBuilder(str.length()*n);
   for(int i = 0;i<n;i++) {
      sb.append(str);
   }
   return sb.toString();
}

same above method with StringBuilder but using String




public String stringTimes(String str, int n) {
   String result = "" ;
   for(int i = 0;i<n;i++) {
       result += str ;
   }
   return result ;
}

Open in new window



How to do byte comparison and see efficiency of each program or operation
0
Comment
Question by:gudii9
7 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 167 total points
Comment Utility
You need a tool to do that.

The Java SDK includes one called "javap" that dumps byte code.  Explained here:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html

Or here's a version that runs inside Eclipse:
http://www.drgarbage.com/bytecode-visualizer/

The "byte code" is what the compiler actually produces from your program.

Doug
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 166 total points
Comment Utility
When you decompile you'll see that in the case where concatenation is used that StringBuilder is used anyway. The key difference is that a new StringBuilder is created in every iteration of the loop and its append and toString methods are called in each iteration too.
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 167 total points
Comment Utility
and see efficiency of each program or operation
You could use a application to time the two different methods.  In my timing application, I see the first method being anywhere from 50 to 250 times faster than the second method. But, generally they both take less than a millisecond to execute.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
But, generally they both take less than a millisecond to execute.
Yes, but the trouble is that the sort of programmers who write the second kind of code are not the kind of programmers who go through their code and totally switch their approach if they're suddenly (say) dealing with massive strings ;)
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
Or here's a version that runs inside Eclipse:
http://www.drgarbage.com/bytecode-visualizer/
it is free plugin right. How to integrate with eclipse. Please advise
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
I went to eclise market place nad install the drgarbage tool. I wonder how to use it. please advise


I followed as below from link but could not see any class files in the navigator view. please advise. How to compare the byte codes of string and string builder methods.?
http://www.drgarbage.com/bytecode-visualizer/class-file-editor/


After compiling a Java source file (with the .java filename extention), the Java Compiler generates a class file (with the .class filename extension), which can be found in the bin folder of a Java project when you are in the Navigator View of Eclipse.

Window > Show View > Other... > General > Navigator
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
Please advise on how to use drgarbage tool to compare two methods.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
object oriented javascript web form 8 71
wordlen challenge 3 46
Java Message handling in Service Layer 3 37
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now