Skip to content

Commit 21f6b4a

Browse files
committed
refactor offsetted series benchmark
1 parent cbc75ac commit 21f6b4a

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

src/main/java/me/lemire/integercompression/BenchmarkOffsettedSeries.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class BenchmarkOffsettedSeries
1313
{
1414
public static final int DEFAULT_MEAN = 1 << 20;
1515
public static final int DEFAULT_RANGE = 1 << 10;
16+
public static final int DEFAULT_REPEAT = 10;
17+
public static final int DEFAULT_WARMUP = 2;
1618

1719
public BenchmarkOffsettedSeries() {
1820
}
@@ -26,9 +28,6 @@ public BenchmarkOffsettedSeries() {
2628
*/
2729
public void run(PrintWriter csvWriter, int count, int length)
2830
{
29-
int[][] data = generateDataChunks(0, count, length,
30-
DEFAULT_MEAN, DEFAULT_RANGE);
31-
3231
IntegerCODEC[] codecs = {
3332
new Composition(new XorBinaryPacking(), new VariableByte()),
3433
new IntegratedComposition(
@@ -42,28 +41,50 @@ public void run(PrintWriter csvWriter, int count, int length)
4241

4342
csvWriter.format("\"Algorithm\",\"Bits per int\"," +
4443
"\"Compress speed (MiS)\",\"Decompress speed (MiS)\"\n");
44+
45+
int[][] randData = generateDataChunks(0, count, length,
46+
DEFAULT_MEAN, DEFAULT_RANGE);
47+
benchmark(csvWriter, "Random (mean=2^20 range=2^10)",
48+
codecs, randData, DEFAULT_REPEAT, DEFAULT_WARMUP);
49+
}
50+
51+
private void benchmark(
52+
PrintWriter csvWriter,
53+
String labelPrefix,
54+
IntegerCODEC[] codecs,
55+
int[][] data,
56+
int repeat,
57+
int warmup)
58+
{
4559
for (IntegerCODEC codec : codecs) {
46-
benchmark(csvWriter, codec.toString(), codec, data);
60+
String label = labelPrefix + " - " + codec.toString();
61+
for (int i = 0; i < warmup; ++i) {
62+
benchmark(null, label, codec, data, repeat);
63+
}
64+
benchmark(csvWriter, label, codec, data, repeat);
4765
}
4866
}
4967

5068
private void benchmark(
5169
PrintWriter csvWriter,
5270
String label,
5371
IntegerCODEC codec,
54-
int[][] data)
72+
int[][] data,
73+
int repeat)
5574
{
5675
PerformanceLogger logger = new PerformanceLogger();
5776

5877
int maxLen = getMaxLen(data);
5978
int[] compressBuffer = new int[4 * maxLen + 1024];
6079
int[] decompressBuffer = new int[maxLen];
6180

62-
for (int[] array : data) {
63-
int compSize = compress(logger, codec, array, compressBuffer);
64-
int decompSize = decompress(logger, codec, compressBuffer,
65-
compSize, decompressBuffer);
66-
checkArray(array, decompressBuffer, decompSize, codec);
81+
for (int i = 0; i < repeat; ++i) {
82+
for (int[] array : data) {
83+
int compSize = compress(logger, codec, array, compressBuffer);
84+
int decompSize = decompress(logger, codec, compressBuffer,
85+
compSize, decompressBuffer);
86+
checkArray(array, decompressBuffer, decompSize, codec);
87+
}
6788
}
6889

6990
if (csvWriter != null) {
@@ -166,7 +187,7 @@ public static void main(String[] args) throws Exception {
166187
+ csvFile.getName());
167188
System.out.println();
168189
BenchmarkOffsettedSeries b = new BenchmarkOffsettedSeries();
169-
b.run(writer, 16 * 1024, 1024);
190+
b.run(writer, 16 * 1000, 1000);
170191
System.out.println();
171192
System.out.println("# Results were written into a CSV file: "
172193
+ csvFile.getName());

src/main/java/me/lemire/integercompression/PerformanceLogger.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public final class PerformanceLogger
88
{
99
public static class Timer {
1010
private long startNano;
11-
private long duration;
11+
private long duration = 0;
1212

1313
public Timer() {}
1414

@@ -17,7 +17,7 @@ public void start() {
1717
}
1818

1919
public long end() {
20-
return this.duration = System.nanoTime() - this.startNano;
20+
return this.duration += System.nanoTime() - this.startNano;
2121
}
2222

2323
public long getDuration() {
@@ -53,11 +53,15 @@ public double getBitPerInt() {
5353
return this.compressedSize * 32.0 / this.originalSize;
5454
}
5555

56+
private static double getMiS(long size, long nanoTime) {
57+
return (size * 1e-6) / (nanoTime * 1.0e-9);
58+
}
59+
5660
public double getCompressSpeed() {
57-
return originalSize * 1.0e3 / this.compressionTimer.getDuration();
61+
return getMiS(this.originalSize, this.compressionTimer.getDuration());
5862
}
5963

6064
public double getDecompressSpeed() {
61-
return originalSize * 1.0e3 / this.decompressionTimer.getDuration();
65+
return getMiS(this.originalSize, this.decompressionTimer.getDuration());
6266
}
6367
}

0 commit comments

Comments
 (0)