Solved

byte comparison and see efficiency of each program

Posted on 2014-10-11
7
147 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
Unable to start eclipse ? 17 128
GUI builder for Eclipse? 8 27
netstat -ano | find "8000" and taskkill /f /pid 2984 3 29
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

932 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

9 Experts available now in Live!

Get 1:1 Help Now