Solved

byte comparison and see efficiency of each program

Posted on 2014-10-11
7
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 167 total points
ID: 40375313
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
ID: 40375543
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
ID: 40377345
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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
LVL 86

Expert Comment

by:CEHJ
ID: 40377368
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
ID: 40377615
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
ID: 40379069
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
ID: 40385756
Please advise on how to use drgarbage tool to compare two methods.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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