Solved

byte comparison and see efficiency of each program

Posted on 2014-10-11
7
152 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 160
Math Equation 23 148
Cisco ASA: Java web start no go, asdm launcher no go 3 88
American Express @Work site and Java 4 95
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

734 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