Commit 0120b22b authored by Moritz Weichert's avatar Moritz Weichert
Browse files

Updated Benchmark

Added Gitignore
Removed Target
parent 116a97e0
.settings
target/
\ No newline at end of file
package de.h_da.stud.wia1;
/*
Taken from https://www.java-tips.org/java-se-tips-100019/24-java-lang/1903-binary-search-tree-implementation-in-java.html
*/
class BinarySearchTree {
Node root;
/**
* Construct the tree.
*/
public BinarySearchTree( ) {
root = null;
}
/**
* Insert into the tree.
* @param x the item to insert.
* @throws DuplicateItemException if x is already present.
*/
public void insert( Comparable x ) {
root = insert( x, root );
}
/**
* Remove from the tree..
* @param x the item to remove.
* @throws ItemNotFoundException if x is not found.
*/
public void remove( Comparable x ) {
root = remove( x, root );
}
/**
* Remove minimum item from the tree.
* @throws ItemNotFoundException if tree is empty.
*/
public void removeMin( ) {
root = removeMin( root );
}
/**
* Find the smallest item in the tree.
* @return smallest item or null if empty.
*/
public Comparable findMin( ) {
return elementAt( findMin( root ) );
}
/**
* Find the largest item in the tree.
* @return the largest item or null if empty.
*/
public Comparable findMax( ) {
return elementAt( findMax( root ) );
}
/**
* Find an item in the tree.
* @param x the item to search for.
* @return the matching item or null if not found.
*/
public Comparable find( Comparable x ) {
return elementAt( find( x, root ) );
}
/**
* Make the tree logically empty.
*/
public void makeEmpty( ) {
root = null;
}
/**
* Test if the tree is logically empty.
* @return true if empty, false otherwise.
*/
public boolean isEmpty( ) {
return root == null;
}
/**
* Internal method to get element field.
* @param t the node.
* @return the element field or null if t is null.
*/
private Comparable elementAt( Node t ) {
return t == null ? null : t.data;
}
/**
* Internal method to insert into a subtree.
* @param x the item to insert.
* @param t the node that roots the tree.
* @return the new root.
* @throws DuplicateItemException if x is already present.
*/
protected Node insert( Comparable x, Node t ) {
if( t == null )
t = new Node( x );
else if( x.compareTo( t.data ) < 0 )
t.left = insert( x, t.left );
else if( x.compareTo( t.data ) > 0 )
t.right = insert( x, t.right );
else
throw new DuplicateItemException( x.toString( ) ); // Duplicate
return t;
}
protected Node remove( Comparable x, Node t ) {
if( t == null )
throw new ItemNotFoundException( x.toString( ) );
if( x.compareTo( t.data ) < 0 )
t.left = remove( x, t.left );
else if( x.compareTo( t.data ) > 0 )
t.right = remove( x, t.right );
else if( t.left != null && t.right != null ) // Two children
{
t.data = findMin( t.right ).data;
t.right = removeMin( t.right );
} else
t = ( t.left != null ) ? t.left : t.right;
return t;
}
/**
* Internal method to remove minimum item from a subtree.
* @param t the node that roots the tree.
* @return the new root.
* @throws ItemNotFoundException if x is not found.
*/
protected Node removeMin( Node t ) {
if( t == null )
throw new ItemNotFoundException( );
else if( t.left != null ) {
t.left = removeMin( t.left );
return t;
} else
return t.right;
}
/**
* Internal method to find the smallest item in a subtree.
* @param t the node that roots the tree.
* @return node containing the smallest item.
*/
protected Node findMin( Node t ) {
if( t != null )
while( t.left != null )
t = t.left;
return t;
}
/**
* Internal method to find the largest item in a subtree.
* @param t the node that roots the tree.
* @return node containing the largest item.
*/
private Node findMax( Node t ) {
if( t != null )
while( t.right != null )
t = t.right;
return t;
}
/**
* Internal method to find an item in a subtree.
* @param x is item to search for.
* @param t the node that roots the tree.
* @return node containing the matched item.
*/
private Node find( Comparable x, Node t ) {
while( t != null ) {
if( x.compareTo( t.data ) < 0 )
t = t.left;
else if( x.compareTo( t.data ) > 0 )
t = t.right;
else
return t; // Match
}
return null; // Not found
}
}
class Node {
Node( Comparable x) {
this.data = x;
left = right = null;
}
Comparable data;
Node left;
Node right;
}
/**
* Exception class for duplicate item errors
* in search tree insertions.
* @author Mark Allen Weiss
*/
class DuplicateItemException extends RuntimeException {
/**
* Construct this exception object.
*/
public DuplicateItemException( ) {
super( );
}
/**
* Construct this exception object.
* @param message the error message.
*/
public DuplicateItemException( String message ) {
super( message );
}
}
/**
* Exception class for failed finds/removes in search
* trees, hash tables, and list and tree iterators.
* @author Mark Allen Weiss
*/
class ItemNotFoundException extends RuntimeException {
/**
* Construct this exception object.
*/
public ItemNotFoundException( ) {
super( );
}
/**
* Construct this exception object.
* @param message the error message.
*/
public ItemNotFoundException( String message ) {
super( message );
}
}
\ No newline at end of file
......@@ -14,18 +14,17 @@ import java.util.concurrent.TimeUnit;
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class MyBenchmark {
public static final int N = 100000;
public static final int N = 1000;
@State(Scope.Thread)
public static class MyState {
LinkedList<Integer> mList = null;
BinarySearchTree mUnbalancedTree = null;
TreeSet<Integer> mTree = null;
@Setup(Level.Trial)
@Setup(Level.Invocation)
public void doSetup() {
System.out.println("Do setup");
mList = new LinkedList<Integer>();
mUnbalancedTree = new BinarySearchTree();
mTree = new TreeSet<Integer>();
}
......@@ -38,16 +37,16 @@ public class MyBenchmark {
@State(Scope.Thread)
public static class DefaultState {
LinkedList<Integer> mList = null;
BinarySearchTree mUnbalancedTree = null;
TreeSet<Integer> mTree = null;
@Setup(Level.Invocation)
public void doSetup() {
mList = new LinkedList<Integer>();
mUnbalancedTree = new BinarySearchTree();
mTree = new TreeSet<Integer>();
for(int i = 0; i < N; i++) {
mList.add(i);
mUnbalancedTree.insert(i);
}
for(int i = 0; i < N; i++) {
......@@ -62,38 +61,51 @@ public class MyBenchmark {
}
}
/*
CREATE
*/
@Benchmark
public void createLinkedList(MyState state) {
public void unbalanced_create(MyState state) {
for(int i = 0; i < N; i++) {
state.mList.add(i);
state.mUnbalancedTree.insert(i);
}
}
@Benchmark
public void createTree(MyState state) {
public void balanced_create(MyState state) {
for(int i = 0; i < N; i++) {
state.mTree.add(i);
}
}
/*
FIND
*/
@Benchmark
public void findMiddleList(DefaultState state) {
state.mList.contains(N/2);
public void unblanaced_findLast(DefaultState state) {
state.mUnbalancedTree.find(N-1);
}
@Benchmark
public void findLastTree(DefaultState state) {
public void blanaced_findLast(DefaultState state) {
state.mTree.contains(N-1);
}
/*
DELETE
*/
@Benchmark
public void deleteMiddleList(DefaultState state) {
state.mList.remove(N/2);
public void unbalanced_deleteLast(DefaultState state) {
state.mUnbalancedTree.remove(N-1);
}
@Benchmark
public void deleteLastTree(DefaultState state) {
public void balanaced_deleteLast(DefaultState state) {
state.mTree.remove(N-1);
}
......
JMH S 29 de.h_da.stud.wia1.MyBenchmark S 58 de.h_da.stud.wia1.generated.MyBenchmark_createTree_jmhTest S 10 createTree S 11 AverageTime E A 1 1 1 E I 1 1 E E I 1 3 E E E E E E E E E U 12 MILLISECONDS E E
JMH S 29 de.h_da.stud.wia1.MyBenchmark S 60 de.h_da.stud.wia1.generated.MyBenchmark_findLastTree_jmhTest S 12 findLastTree S 11 AverageTime E A 1 1 1 E I 1 1 E E I 1 3 E E E E E E E E E U 12 MILLISECONDS E E
JMH S 29 de.h_da.stud.wia1.MyBenchmark S 64 de.h_da.stud.wia1.generated.MyBenchmark_deleteMiddleList_jmhTest S 16 deleteMiddleList S 11 AverageTime E A 1 1 1 E I 1 1 E E I 1 3 E E E E E E E E E U 12 MILLISECONDS E E
JMH S 29 de.h_da.stud.wia1.MyBenchmark S 62 de.h_da.stud.wia1.generated.MyBenchmark_findMiddleList_jmhTest S 14 findMiddleList S 11 AverageTime E A 1 1 1 E I 1 1 E E I 1 3 E E E E E E E E E U 12 MILLISECONDS E E
JMH S 29 de.h_da.stud.wia1.MyBenchmark S 62 de.h_da.stud.wia1.generated.MyBenchmark_deleteLastTree_jmhTest S 14 deleteLastTree S 11 AverageTime E A 1 1 1 E I 1 1 E E I 1 3 E E E E E E E E E U 12 MILLISECONDS E E
JMH S 29 de.h_da.stud.wia1.MyBenchmark S 64 de.h_da.stud.wia1.generated.MyBenchmark_createLinkedList_jmhTest S 16 createLinkedList S 11 AverageTime E A 1 1 1 E I 1 1 E E I 1 3 E E E E E E E E E U 12 MILLISECONDS E E
dontinline,*.*_all_jmhStub
dontinline,*.*_avgt_jmhStub
dontinline,*.*_sample_jmhStub
dontinline,*.*_ss_jmhStub
dontinline,*.*_thrpt_jmhStub
inline,de/h_da/stud/wia1/MyBenchmark$DefaultState.doSetup
inline,de/h_da/stud/wia1/MyBenchmark$DefaultState.doTearDown
inline,de/h_da/stud/wia1/MyBenchmark$MyState.doSetup
inline,de/h_da/stud/wia1/MyBenchmark$MyState.doTearDown
inline,de/h_da/stud/wia1/MyBenchmark.createLinkedList
inline,de/h_da/stud/wia1/MyBenchmark.createTree
inline,de/h_da/stud/wia1/MyBenchmark.deleteLastTree
inline,de/h_da/stud/wia1/MyBenchmark.deleteMiddleList
inline,de/h_da/stud/wia1/MyBenchmark.findLastTree
inline,de/h_da/stud/wia1/MyBenchmark.findMiddleList
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment