Performance Zone is brought to you in partnership with:

Computer Science Engineer having more than 14 years of experience, predominantly in the Enterprise Product Development Space. Professionally started with LabWindows, then to X-Windows/Motif and landed in using Java. Primitive experience in VC++ and .Net. Making hands dirty with DSLs in the free time (?) For more about me, visit http://jagannathan.info Jagannathan is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

perf4j – Sample Snippets

01.23.2013
| 14658 views |
  • submit to reddit

 All samples depend on the Perf4J.


import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;
 
public class ArrayCopy {
 
private static int count = 200000;
private static void nArrayCopy() {
StopWatch stopWatch = new Log4JStopWatch();
int arrayFirst[]= new int[count];
int arraySecond[] = new int[count];
 
for(int i=0;i<count;i++) {
arrayFirst[i] = i;
}
 
for(int i=0; i<count ; i++) {
arraySecond[i] = arrayFirst[i];
}
 
for(int i=0; i<count ; i++) {
if(i/5000 == 0)
System.out.print(arraySecond[i]);
}
stopWatch.stop(“Normal Array Copy”);
 
}
 
private static void sArrayCopy() {
StopWatch stopWatch = new Log4JStopWatch();
int arrayFirst[]= new int[count];
int arraySecond[] = new int[count];
 
for(int i=0;i<count;i++) {
arrayFirst[i] = i;
}
 
 
System.arraycopy(arrayFirst, 0, arraySecond, 0, count);
 
for(int i=0; i<count ; i++) {
if(i/5000 == 0)
System.out.print(arraySecond[i]);
}
stopWatch.stop(“System Array Copy”);
 
}
 
public static void main(String[] args) {
nArrayCopy();
sArrayCopy();
}
}


import java.util.HashMap;
import java.util.Hashtable;
 
import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;
 
public class HMHT {
 
private static int count = 2000;
 
private static void hashMapMan() {
StopWatch stopWatch = new Log4JStopWatch();
HashMap hMan = new HashMap();
for(int i=0;i<count;i++) {
hMan.put(i, i);
}
stopWatch.stop(“HashMap”);
}
 
private static void hashTableMan() {
StopWatch stopWatch = new Log4JStopWatch();
Hashtable hTable = new Hashtable();
for(int i=0;i<count;i++) {
hTable.put(i, i);
}
stopWatch.stop(“HashTable”);
}
 
public static void main(String[] args) {
hashMapMan();
hashTableMan();
}
 
}


import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;
 
public class Primitivies {
 
private static int count = 20000;
 
private static void usingChar() {
StopWatch stopWatch = new Log4JStopWatch();
char val=0;
for(char i=0; i<count;i++) {
val = (char) (val + i);
}
stopWatch.stop(“Char”);
 
}
 
private static void usingShort() {
StopWatch stopWatch = new Log4JStopWatch();
short val=0;
for(short i=0; i<count;i++) {
val = (short) (val + i);
}
stopWatch.stop(“Short”);
}
 
private static void usingInt() {
StopWatch stopWatch = new Log4JStopWatch();
int val=0;
for(int i=0; i<count;i++) {
val = val + i;
}
stopWatch.stop(“Int”);
}
 
public static void main(String[] args) {
usingChar();
usingShort();
usingInt();
}
}


import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;
 
public class StringMan {
 
private static int count = 2000;
 
public static void usingString() {
StopWatch stopWatch = new Log4JStopWatch();
String output= new String();
for(int i=0;i<count;i++) {
output = output.concat(Integer.toString(i));
}
stopWatch.stop(“String”);
}
 
public static void usingStringBuffer() {
StopWatch stopWatch = new Log4JStopWatch();
StringBuffer output= new StringBuffer();
for(int i=0;i<count;i++) {
output = output.append(Integer.toString(i));
}
stopWatch.stop(“StringBuffer”);
}
 
public static void main(String[] args) {
usingString();
usingStringBuffer();
}
}


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
 
import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;
 
public class VALHM {
 
private static int count = 2000;
 
private static void vectorMan() {
StopWatch stopWatch = new Log4JStopWatch();
Vector vMan = new Vector();
for(int i=0;i<count;i++) {
vMan.add(i);
}
stopWatch.stop(“Vector”);
}
 
private static void hashMapMan() {
StopWatch stopWatch = new Log4JStopWatch();
HashMap hMan = new HashMap();
for(int i=0;i<count;i++) {
hMan.put(i, i);
}
stopWatch.stop(“HashMap”);
}
 
private static void arrayListMan() {
StopWatch stopWatch = new Log4JStopWatch();
ArrayList aMan = new ArrayList();
for(int i=0;i<count;i++) {
aMan.add(i);
}
stopWatch.stop(“ArrayList”);
}
 
public static void main(String[] args) {
vectorMan();
hashMapMan();
arrayListMan();
}
}





Published at DZone with permission of Jagannathan Asokan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Xavier Dury replied on Wed, 2013/01/23 - 4:24am

This comment uses ROT13 .

public class Comment {

	public static void main(String[] args) {
		String s = "V qba'g haqrefgnaq ubj guvf xvaq bs negvpyrf pna or choyvfurq ba QMbar.\nQMbar vf orpbzvat gur arj Cnfgrova...";
		for (char c : s.toCharArray()) {
			if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M')) {
				c += 13;
			} else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z')) {
				c -= 13;
			}
			System.out.print(c);
		}
	}
}

Tomasz Nurkiewicz replied on Wed, 2013/01/23 - 1:56pm in response to: Xavier Dury

+1, and for those, who are lazy ...

Jakub B?aszczyk replied on Sat, 2013/01/26 - 5:06pm

ArrayCopy sample posted twice, or am I missing something?

Jagannathan Asokan replied on Sun, 2013/01/27 - 9:18pm in response to: Jakub B?aszczyk

Rectified and thanks for the feedback


Regards,

Jagan

Aaron Korver replied on Wed, 2013/01/30 - 4:27pm

Thanks for your insightful thoughts on this.  It really makes me rethink my thoughts.  I hope that you can provide more outstanding insight.  

This article is about code formatting right?  :-)

Simon Massey replied on Wed, 2013/03/13 - 12:02pm in response to: Tomasz Nurkiewicz

Awesome! ideone looks very cool. Thanks for putting in a little thought, talent and effort to light up this otherwise bleak page. 

Michael Je replied on Fri, 2013/04/05 - 7:40am

 ArrayCopy sample posted twice, or am I missing something?

www.babul-ilm.com

Sumit Narayan replied on Fri, 2013/04/19 - 5:46am

 This one was really helpful. I was stuck with the same and your snippet has helped me in a great way

http://www.ifeel.edu.in

Stuart Smith replied on Fri, 2013/04/19 - 11:19am

I was stucked in between of this code and you helped me in great way. Thank you

http://www.venkotech.com 

Junio Mousull replied on Mon, 2013/05/20 - 6:09pm in response to: Xavier Dury

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.