?
Solved

byte comparison and see efficiency of each program

Posted on 2014-10-11
7
Medium Priority
?
159 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 28

Accepted Solution

by:
dpearson earned 668 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 664 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 28

Assisted Solution

by:rrz
rrz earned 668 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

777 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