Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Issue #27 : Added Fastutil object bench
Browse files Browse the repository at this point in the history
+ fixed Benchmarks again, benchmarking is hard dammit !
  • Loading branch information
vsonnier committed Feb 1, 2015
1 parent 03faeef commit 9bcae04
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.carrotsearch.hppcrt;

import java.util.Arrays;
import java.util.Random;

import com.carrotsearch.hppcrt.lists.IntArrayList;

public class DistributionGenerator
{
public abstract class Generator
Expand All @@ -20,57 +23,169 @@ public int[] prepare(final int size)
public abstract int getNext();
}

public final Generator RANDOM = new Generator() {

@Override
public int getNext()
{
return DistributionGenerator.this.prng.nextInt(DistributionGenerator.this.maxSize);
}
};
private final Random prng;
private final int targetSize;
private final int initValue;

public final Generator LINEAR = new Generator() {
//////////////////////////////////
//Generator kinds, enum-like patterns
/////////////////////////////////

@Override
public int getNext()
{
DistributionGenerator.this.initValue++;
return 2 * (DistributionGenerator.this.initValue + 1);
}
};
/**
* Generate Random numbers between [initValue; initValue + targetSize]
*/
public final Generator RANDOM;

public final Generator LINEAR_DECREMENT = new Generator() {
/**
* Generate LINEAR increasing numbers by steps of 3, between [initValue; initValue + ( 3* targetSize)]
* so a set of roughly targetSize values
*/
public final Generator LINEAR;

@Override
public int getNext()
{
DistributionGenerator.this.initValue++;
return DistributionGenerator.this.maxSize - DistributionGenerator.this.initValue - 2;
}
};
/**
* Generate LINEAR decreasing numbers by steps of 3, between [ initValue + ( 3* targetSize) ; initValue]
* so a set of roughly targetSize values
*/
public final Generator LINEAR_DECREMENT;

/**
* Linear increments on 12 high bits first, then on lower bits.
* (target size is not used)
*/
public final Generator HIGHBITS = new Generator() {
public final Generator HIGHBITS;

@Override
public int getNext()
{
DistributionGenerator.this.initValue++;
return DistributionGenerator.this.initValue << 32 - 12 | DistributionGenerator.this.initValue >>> 12;
}
};
/**
* List of generators
* @param args
*/
public final Generator[] GENERATORS;

private final Random prng;
/**
* Constructor
* @param targetSize
* @param randomGene
*/
public DistributionGenerator(final int initialValue, final int targetSize, final Random randomGene)
{
this.targetSize = targetSize;
this.prng = randomGene;
this.initValue = initialValue;

private final int maxSize;
//We must construct the generators here after DistributionGenerator init:
this.RANDOM = new Generator() {

private int initValue = 1;
int counter = 0;

public DistributionGenerator(final int maxSize, final Random randomGene)
{
this.maxSize = maxSize;
this.prng = randomGene;
@Override
public int getNext()
{
this.counter++;
return DistributionGenerator.this.prng.nextInt(DistributionGenerator.this.targetSize) + DistributionGenerator.this.initValue;
}

@Override
public String toString() {

return "Generator(RANDOM, counter = " + this.counter + ")";
}
};

this.LINEAR = new Generator() {

int counter = 0;

@Override
public int getNext()
{
final int value = DistributionGenerator.this.initValue + (3 * this.counter);

this.counter = (this.counter + 1) % DistributionGenerator.this.targetSize;

return value;
}

@Override
public String toString() {

return "Generator(LINEAR, counter = " + this.counter + ")";
}
};

this.LINEAR_DECREMENT = new Generator() {

int counter = 0;

int startValue = DistributionGenerator.this.initValue + (3 * DistributionGenerator.this.targetSize);

@Override
public int getNext()
{
final int value = this.startValue - 3 * this.counter;

this.counter = (this.counter + 1) % DistributionGenerator.this.targetSize;

return value;
}

@Override
public String toString() {

return "Generator(LINEAR_DECREMENT, counter = " + this.counter + ")";
}
};

this.HIGHBITS = new Generator() {

int counter = 0;

@Override
public int getNext()
{
final int value = (this.counter << (32 - 12)) | (this.counter >>> 12);
this.counter++;

return value;
}

@Override
public String toString() {

return "Generator(HIGHBITS, counter = " + this.counter + ")";
}
};

this.GENERATORS = new Generator[] { this.RANDOM, this.LINEAR, this.LINEAR_DECREMENT, this.HIGHBITS };
}

/////////////////////////////////
//main for tests
/////////////////////////////////

public static void main(final String[] args) {

final DistributionGenerator testDistrib = new DistributionGenerator(-100, 200, new XorShiftRandom(1234789));
final DistributionGenerator testDistribPrepare = new DistributionGenerator(-100, 200, new XorShiftRandom(1234789));

for (int jj = 0; jj < testDistrib.GENERATORS.length; jj++) {

final Generator gene = testDistrib.GENERATORS[jj];

System.out.println(">>>> Test generator :" + gene.toString());

final IntArrayList generatedValues = new IntArrayList();

for (int ii = 0; ii < 200; ii++) {

generatedValues.add(gene.getNext());
}

System.out.println(">>>> Test iterated getNext(), size = " + generatedValues.size() + " :\n" + generatedValues.toString());

//test bulk:
final int[] prepared = testDistribPrepare.GENERATORS[jj].prepare(200);
System.out.println(">>>> Test bulk prepare(), size = " + prepared.length + " :\n" + Arrays.toString(prepared));
System.out.println("");

} //end for
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.carrotsearch.hppcrt;

import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;

import java.util.Random;

public class FastUtilObjectMap extends MapImplementation<Object2IntOpenHashMap<MapImplementation.ComparableInt>>
{

private ComparableInt[] insertKeys;
private ComparableInt[] containsKeys;
private ComparableInt[] removedKeys;
private int[] insertValues;

protected FastUtilObjectMap(final int size, final float loadFactor)
{
super(new Object2IntOpenHashMap<ComparableInt>(size, loadFactor));
}

/**
* Setup
*/
@Override
public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) {

final Random prng = new XorShiftRandom(0x122335577L);

this.insertKeys = new ComparableInt[keysToInsert.length];

this.containsKeys = new ComparableInt[keysForContainsQuery.length];
this.removedKeys = new ComparableInt[keysForRemovalQuery.length];

this.insertValues = new int[keysToInsert.length];

//Auto box into Integers, they must have the same length anyway.
for (int i = 0; i < keysToInsert.length; i++) {

this.insertKeys[i] = new ComparableInt(keysToInsert[i], hashQ);

this.insertValues[i] = prng.nextInt();
}

//Auto box into Integers
for (int i = 0; i < keysForContainsQuery.length; i++) {

this.containsKeys[i] = new ComparableInt(keysForContainsQuery[i], hashQ);
}

//Auto box into Integers
for (int i = 0; i < keysForRemovalQuery.length; i++) {

this.removedKeys[i] = new ComparableInt(keysForRemovalQuery[i], hashQ);
}

//don't make things too easy, shuffle it so the bench do some pointer chasing in memory.
//for the inserted keys

Util.shuffle(this.insertKeys, prng);
}

@Override
public void clear() {
this.instance.clear();
}

@Override
public int size() {

return this.instance.size();
}

@Override
public int benchPutAll() {

final Object2IntOpenHashMap<ComparableInt> instance = this.instance;
final int[] values = this.insertValues;

int count = 0;

final ComparableInt[] keys = this.insertKeys;

for (int i = 0; i < keys.length; i++) {

count += instance.put(keys[i], values[i]);
}

return count;
}

@Override
public int benchContainKeys()
{
final Object2IntOpenHashMap<ComparableInt> instance = this.instance;

int count = 0;

final ComparableInt[] keys = this.containsKeys;

for (int i = 0; i < keys.length; i++) {

count += instance.containsKey(keys[i]) ? 1 : 0;
}

return count;
}

@Override
public int benchRemoveKeys() {

final Object2IntOpenHashMap<ComparableInt> instance = this.instance;

int count = 0;

final ComparableInt[] keys = this.removedKeys;

for (int i = 0; i < keys.length; i++) {

count += instance.removeInt(keys[i]);
}

return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY
}

//don't make things too easy, shuffle it so the bench do some pointer chasing in memory.
//for the inserted keys

Util.shuffle(this.insertKeys, prng);
Util.shuffle(this.containsKeys, prng);
Util.shuffle(this.removedKeys, prng);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY
}

//don't make things too easy, shuffle it so the bench do some pointer chasing in memory.
//for the inserted keys

Util.shuffle(this.insertKeys, prng);
Util.shuffle(this.containsKeys, prng);
Util.shuffle(this.removedKeys, prng);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public MapImplementation<?> getInstance(final int size, final float loadFactor)
}
},

FASTUTIL_OBJECT
{
@Override
public MapImplementation<?> getInstance(final int size, final float loadFactor)
{
return new FastUtilObjectMap(size, loadFactor);
}
},

MAHOUT
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY
}

//don't make things too easy, shuffle it so the bench do some pointer chasing in memory.
//for the inserted keys

Util.shuffle(this.insertKeys, prng);
Util.shuffle(this.insertValues, prng);
Util.shuffle(this.containsKeys, prng);
Util.shuffle(this.removedKeys, prng);

}

Expand Down
Loading

0 comments on commit 9bcae04

Please sign in to comment.