Skip to content

Instantly share code, notes, and snippets.

@stevenschlansker
Created December 21, 2012 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenschlansker/4356621 to your computer and use it in GitHub Desktop.
Save stevenschlansker/4356621 to your computer and use it in GitHub Desktop.
package com.nesscomputing.uuid;
import java.util.UUID;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
public class PerformanceComparison extends SimpleBenchmark
{
private static final int N_UUIDS = 1000;
private static final UUID[] testUuids;
private static final String[] testStrings;
static {
testUuids = new UUID[N_UUIDS];
testStrings = new String[N_UUIDS];
for (int i = 0; i < N_UUIDS; i++)
{
testUuids[i] = UUID.randomUUID();
testStrings[i] = testUuids[i].toString();
}
}
public static void main(String[] args)
{
Runner.main(PerformanceComparison.class, args);
}
public long timeJdkUuidFromString(int reps)
{
long accum = 0;
for (int i = 0; i < reps; i++)
{
accum += UUID.fromString(testStrings[i % N_UUIDS]).getMostSignificantBits();
}
return accum;
}
public long timeJdkUuidToString(int reps)
{
long accum = 0;
for (int i = 0; i < reps; i++)
{
accum += testUuids[i % N_UUIDS].toString().charAt(0);
}
return accum;
}
public long timeNessUuidFromString(int reps)
{
long accum = 0;
for (int i = 0; i < reps; i++)
{
accum += NessUUID.fromString(testStrings[i % N_UUIDS]).getMostSignificantBits();
}
return accum;
}
public long timeNessUuidToString(int reps)
{
long accum = 0;
for (int i = 0; i < reps; i++)
{
accum += NessUUID.toString(testUuids[i % N_UUIDS]).charAt(0);
}
return accum;
}
}
0% Scenario{vm=java, trial=0, benchmark=JdkUuidFromString} 592.45 ns; σ=1.50 ns @ 3 trials
25% Scenario{vm=java, trial=0, benchmark=JdkUuidToString} 318.25 ns; σ=1.91 ns @ 3 trials
50% Scenario{vm=java, trial=0, benchmark=NessUuidFromString} 181.36 ns; σ=1.76 ns @ 3 trials
75% Scenario{vm=java, trial=0, benchmark=NessUuidToString} 47.58 ns; σ=0.17 ns @ 3 trials
benchmark ns linear runtime
JdkUuidFromString 592.5 ==============================
JdkUuidToString 318.2 ================
NessUuidFromString 181.4 =========
NessUuidToString 47.6 ==
vm: java
trial: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment