Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

perf4j – Sample Snippets

DZone's Guide to

perf4j – Sample Snippets

Free Resource

Download our Introduction to API Performance Testing and learn why testing your API is just as important as testing your website, and how to start today.

 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();
}
}





Find scaling and performance issues before your customers do with our Introduction to High-Capacity Load Testing guide.

Topics:

Published at DZone with permission of Jagannathan Asokan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}