Skip to content

Instantly share code, notes, and snippets.

@QIvan
Created June 14, 2017 18:16
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 QIvan/4e1e0a9579912faadefaf31d9c9c42c1 to your computer and use it in GitHub Desktop.
Save QIvan/4e1e0a9579912faadefaf31d9c9c42c1 to your computer and use it in GitHub Desktop.
kotlin range
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@Fork(1)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
public class KotlinRangeBenchmark {
@Param({"0", "123", "1000000", "1073741823", "2147483640", "2147483647"})
private Integer param;
@Param({"1005000", "2147483647"})
private int rightBound;
private Segment segment;
private SegmentJava segmentJava;
@Setup
public void setup() {
segment = new Segment(0, rightBound);
segmentJava = new SegmentJava(0, rightBound);
}
@Benchmark
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public boolean comparisonWithCheck() {
return segment.pointInsideComparisonWithCheck(param);
}
@Benchmark
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public boolean rangeWithCheck() {
return segment.pointInsideRangeWithCheck(param);
}
@Benchmark
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public boolean range() {
return segment.pointInsideRange(param);
}
@Benchmark
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public boolean java() {
return segmentJava.pointInside(param);
}
}
Benchmark (param) (rightBound) Mode Samples Score Score error Units
m.KotlinRangeBenchmark.comparisonWithCheck 0 1005000 thrpt 5 483574.051 6920.287 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 0 2147483647 thrpt 5 485546.310 1312.583 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 123 1005000 thrpt 5 475247.745 4341.617 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 123 2147483647 thrpt 5 472085.097 10779.033 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 1000000 1005000 thrpt 5 475617.517 1948.904 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 1000000 2147483647 thrpt 5 473975.814 2849.188 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 1073741823 1005000 thrpt 5 471031.757 5945.724 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 1073741823 2147483647 thrpt 5 476453.176 449.730 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 2147483640 1005000 thrpt 5 472104.434 1171.407 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 2147483640 2147483647 thrpt 5 475818.884 939.923 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 2147483647 1005000 thrpt 5 471276.693 4104.048 ops/ms
m.KotlinRangeBenchmark.comparisonWithCheck 2147483647 2147483647 thrpt 5 474723.886 10662.491 ops/ms
m.KotlinRangeBenchmark.java 0 1005000 thrpt 5 474827.095 9314.464 ops/ms
m.KotlinRangeBenchmark.java 0 2147483647 thrpt 5 473643.906 10603.714 ops/ms
m.KotlinRangeBenchmark.java 123 1005000 thrpt 5 464981.836 84418.882 ops/ms
m.KotlinRangeBenchmark.java 123 2147483647 thrpt 5 475409.914 3167.809 ops/ms
m.KotlinRangeBenchmark.java 1000000 1005000 thrpt 5 450744.068 170167.135 ops/ms
m.KotlinRangeBenchmark.java 1000000 2147483647 thrpt 5 474438.403 7486.278 ops/ms
m.KotlinRangeBenchmark.java 1073741823 1005000 thrpt 5 470981.782 6929.155 ops/ms
m.KotlinRangeBenchmark.java 1073741823 2147483647 thrpt 5 474189.849 10766.970 ops/ms
m.KotlinRangeBenchmark.java 2147483640 1005000 thrpt 5 471881.928 1162.924 ops/ms
m.KotlinRangeBenchmark.java 2147483640 2147483647 thrpt 5 475844.138 3612.104 ops/ms
m.KotlinRangeBenchmark.java 2147483647 1005000 thrpt 5 470656.298 8323.160 ops/ms
m.KotlinRangeBenchmark.java 2147483647 2147483647 thrpt 5 475970.759 1800.132 ops/ms
m.KotlinRangeBenchmark.range 0 1005000 thrpt 5 17152.528 17.450 ops/ms
m.KotlinRangeBenchmark.range 0 2147483647 thrpt 5 17181.775 141.423 ops/ms
m.KotlinRangeBenchmark.range 123 1005000 thrpt 5 9168.400 146.442 ops/ms
m.KotlinRangeBenchmark.range 123 2147483647 thrpt 5 9013.292 729.822 ops/ms
m.KotlinRangeBenchmark.range 1000000 1005000 thrpt 5 0.237 0.047 ops/ms
m.KotlinRangeBenchmark.range 1000000 2147483647 thrpt 5 0.239 0.047 ops/ms
m.KotlinRangeBenchmark.range 1073741823 1005000 thrpt 5 0.247 0.056 ops/ms
m.KotlinRangeBenchmark.range 1073741823 2147483647 thrpt 5 0.000 0.000 ops/ms
m.KotlinRangeBenchmark.range 2147483640 1005000 thrpt 5 0.248 0.048 ops/ms
m.KotlinRangeBenchmark.range 2147483640 2147483647 thrpt 5 0.000 0.000 ops/ms
m.KotlinRangeBenchmark.range 2147483647 1005000 thrpt 5 0.245 0.060 ops/ms
m.KotlinRangeBenchmark.range 2147483647 2147483647 thrpt 5 0.000 0.000 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 0 1005000 thrpt 5 484967.240 13519.759 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 0 2147483647 thrpt 5 486319.732 2318.383 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 123 1005000 thrpt 5 486596.469 817.215 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 123 2147483647 thrpt 5 483059.951 10142.874 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 1000000 1005000 thrpt 5 485722.943 2704.185 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 1000000 2147483647 thrpt 5 484675.076 15521.748 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 1073741823 1005000 thrpt 5 485166.040 6240.158 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 1073741823 2147483647 thrpt 5 484290.270 11677.839 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 2147483640 1005000 thrpt 5 481276.015 12541.818 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 2147483640 2147483647 thrpt 5 485601.811 1572.481 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 2147483647 1005000 thrpt 5 482057.115 9245.777 ops/ms
m.KotlinRangeBenchmark.rangeWithCheck 2147483647 2147483647 thrpt 5 485515.129 5229.331 ops/ms
data class Segment(val start: Int, val end: Int) {
fun pointInsideComparisonWithCheck(point: Int?): Boolean {
if (point != null) {
return start <= point && point <= end
} else {
return false
}
}
fun pointInsideRangeWithCheck(point: Int?): Boolean {
if (point != null) {
return point in start..end
} else {
return false
}
}
fun pointInsideRange(point: Int?): Boolean {
return point in start..end
}
}
public class SegmentJava {
private final int start;
private final int end;
public SegmentJava(int start, int end) {
this.start = start;
this.end = end;
}
public boolean pointInside(Integer point) {
if (point != null) {
return start <= point && point <= end;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment