Skip to content
Permalink
Browse files
Benchmark using a long to get an index, compare it, and return the index
  • Loading branch information
aherbert committed Aug 24, 2021
1 parent a39ce5d commit e9382605444503e0309b3b7d60669d8901c700cd
Showing 1 changed file with 76 additions and 0 deletions.
@@ -151,6 +151,68 @@ public void setup() {
}
}

/**
* Defines method to use for creating an index values from a random long and comparing
* it to an {@code int} limit.
*/
@State(Scope.Benchmark)
public static class IndexCompareSources {
/** The method to compare the index against the limit. */
@Param({"CastMaskIntCompare", "MaskCastIntCompare", "MaskLongCompareCast"})
private String method;

/** The sampler. */
private DiscreteSampler sampler;

/**
* @return the sampler.
*/
public DiscreteSampler getSampler() {
return sampler;
}

/** Instantiates sampler. */
@Setup
public void setup() {
// The limit:
// exponential = 252
// gaussian = 253
final int limit = 253;
// Use a fast generator
final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create();
if ("CastMaskIntCompare".equals(method)) {
sampler = () -> {
final long x = rng.nextLong();
final int i = ((int) x) & 0xff;
if (i < limit) {
return i;
}
return 0;
};
} else if ("MaskCastIntCompare".equals(method)) {
sampler = () -> {
final long x = rng.nextLong();
final int i = (int) (x & 0xff);
if (i < limit) {
return i;
}
return 0;
};
} else if ("MaskLongCompareCast".equals(method)) {
sampler = () -> {
final long x = rng.nextLong();
final long i = x & 0xff;
if (i < limit) {
return (int) i;
}
return 0;
};
} else {
throwIllegalStateException(method);
}
}
}

/**
* Defines method to use for creating unsigned {@code long} values.
*/
@@ -3585,6 +3647,20 @@ public int getIndex(IndexSources sources) {
return sources.getSampler().sample();
}

/**
* Benchmark methods for obtaining an index from the lower bits of a long and
* comparing them to a limit then returning the index as an {@code int}.
*
* <p>Note: This is disabled as there is no measurable difference between methods.
*
* @param sources Source of randomness.
* @return the sample value
*/
//@Benchmark
public int compareIndex(IndexCompareSources sources) {
return sources.getSampler().sample();
}

/**
* Benchmark methods for obtaining an unsigned long.
*

0 comments on commit e938260

Please sign in to comment.