Last active
March 19, 2017 20:52
-
-
Save picpromusic/4b19c718bec5a652731a65c7720ac5f8 to your computer and use it in GitHub Desktop.
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
# Run complete. Total time: 00:40:31 | |
Benchmark Mode Cnt Score Error Units | |
MyBenchmark.testNamedPredicate thrpt 200 45938970,625 ± 615390,483 ops/s | |
MyBenchmark.testPredicate thrpt 200 23062083,641 ± 154933,675 ops/s | |
MyBenchmark.testPredicateReal thrpt 200 48308347,165 ± 395810,356 ops/s | |
MyBenchmark.testToString thrpt 200 138366708,182 ± 1177786,195 ops/s | |
MyBenchmark.testToStringNamed thrpt 200 252872229,907 ± 8044289,516 ops/s | |
MyBenchmark.testToStringReal thrpt 200 6670148,202 ± 40200,984 ops/s | |
------- MyBenchmark.java ---- | |
/* | |
* Copyright (c) 2014, Oracle America, Inc. | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* | |
* * Redistributions of source code must retain the above copyright notice, | |
* this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* | |
* * Neither the name of Oracle nor the names of its contributors may be used | |
* to endorse or promote products derived from this software without | |
* specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
* THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
package inc.lambdahelpers; | |
import java.util.function.Predicate; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
@State(Scope.Benchmark) | |
public class MyBenchmark { | |
private Predicate<String> realP = (s) -> !s.trim().isEmpty(); | |
private Predicate<String> p = LambdaNamer.nameIt("isNotEmpty", realP); | |
private Predicate<String> namedP = LambdaNamer.namedPredicate("isNotEmpty", realP); | |
private String testString1 = "Hello"; | |
private String testString2 = " "; | |
@Benchmark | |
public int testPredicate() { | |
int result = 0; | |
result += p.test(testString1) ? 1 : 0 ; | |
result += p.test(testString2) ? 1 : 0 ; | |
return result; | |
} | |
@Benchmark | |
public int testPredicateReal() { | |
int result = 0; | |
result += realP.test(testString1) ? 1 : 0 ; | |
result += realP.test(testString2) ? 1 : 0 ; | |
return result; | |
} | |
@Benchmark | |
public int testNamedPredicate() { | |
int result = 0; | |
result += namedP.test(testString1) ? 1 : 0 ; | |
result += namedP.test(testString2) ? 1 : 0 ; | |
return result; | |
} | |
@Benchmark | |
public String testToString() { | |
return p.toString(); | |
} | |
@Benchmark | |
public String testToStringReal() { | |
return realP.toString(); | |
} | |
@Benchmark | |
public String testToStringNamed() { | |
return namedP.toString(); | |
} | |
} | |
-----LambdaNamer.java-------- | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Proxy; | |
/** | |
* Helper Class to give lambda a name ("toString") for debugging purpose | |
* | |
*/ | |
public class LambdaNamer { | |
private static Method TO_STRING; | |
static { | |
try { | |
TO_STRING = Object.class.getMethod("toString"); | |
} catch (NoSuchMethodException | SecurityException e) { | |
throw new RuntimeException("There is something rotten in state of denmark!"); | |
} | |
} | |
/** | |
* Overrides toString "Method" for a given lambda. | |
* | |
* @param name toString result of lambda | |
* @param obj the lambda to encapsulate | |
* @return the named lambda | |
*/ | |
public static <T> T nameIt(String name, T obj) { | |
if (Boolean.getBoolean("namedLambdasEnabled")) { | |
Class<T> clazz = (Class<T>) obj.getClass(); | |
Class<?>[] interfaces = clazz.getInterfaces(); | |
return (T) Proxy.newProxyInstance(// | |
obj.getClass().getClassLoader(),// | |
interfaces, // | |
(Object proxy, Method method, Object[] args) -> { | |
if (TO_STRING.equals(method)) { | |
return name; | |
} else { | |
return method.invoke(obj, args); | |
} | |
}); | |
} else { | |
return obj; | |
} | |
} | |
public static <T> Predicate<T> namedPredicate(String name, Predicate<? super T> pred) { | |
return new Predicate<T>() { | |
public String toString() { return name; } | |
public boolean test(T t) { return pred.test(t); } | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment