Thursday, May 05, 2011

Some Sample Code

All samples depend on the Perf4J.

- no title specified

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

}

}

No comments: