Created
April 10, 2012 21:47
-
-
Save lowasser/2354834 to your computer and use it in GitHub Desktop.
Benchmark for Funcito-generated functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.caliper.Runner; | |
import com.google.caliper.SimpleBenchmark; | |
import com.google.common.base.Function; | |
import com.google.common.collect.ImmutableList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Random; | |
import org.funcito.Funcito; | |
import org.funcito.FuncitoGuava; | |
public class FunctionBenchmark extends SimpleBenchmark { | |
private List<List<Object>> lists; | |
@Override | |
protected void setUp() throws Exception { | |
Random rng = new Random(); | |
ImmutableList.Builder<List<Object>> builder = ImmutableList.builder(); | |
for (int i = 0; i < 0x10000; i++) { | |
int n = rng.nextInt(0x100); | |
builder.add(Collections.nCopies(n, new Object())); | |
} | |
lists = builder.build(); | |
} | |
public int timeExplicit(int reps) { | |
int tmp = 0; | |
for (int i = 0; i < reps; i++) { | |
Integer len = lists.get(i & 0xffff).size(); | |
tmp += len; | |
} | |
return tmp; | |
} | |
public int timeFuncito(int reps) { | |
int tmp = 0; | |
Function<List<Object>, Integer> sizeFunction = FuncitoGuava.functionFor(Funcito.callsTo( | |
List.class).size()); | |
for (int i = 0; i < reps; i++) { | |
Integer len = sizeFunction.apply(lists.get(i & 0xffff)); | |
tmp += len; | |
} | |
return tmp; | |
} | |
public int timeExplicitFunction(int reps) { | |
int tmp = 0; | |
Function<List<Object>, Integer> sizeFunction = new Function<List<Object>, Integer>() { | |
public Integer apply(List<Object> input) { | |
return input.size(); | |
} | |
}; | |
for (int i = 0; i < reps; i++) { | |
Integer len = sizeFunction.apply(lists.get(i & 0xffff)); | |
tmp += len; | |
} | |
return tmp; | |
} | |
public static void main(String[] args) { | |
Runner.main(FunctionBenchmark.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment