Last active
July 20, 2017 05:52
-
-
Save ecki/399136f4fd59c1d110c1 to your computer and use it in GitHub Desktop.
JMH int to String Benchmark
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
> mvn clean package | |
> java -jar target/benchmark.jarar -r 10 -f 3 | |
... | |
# Run complete. Total time: 00:51:31 | |
Benchmark (n) Mode Cnt Score Error Units | |
ToString.IntPlusString 0 thrpt 9 25,904 ± 1,751 ops/us | |
ToString.IntPlusString 1 thrpt 9 28,621 ± 1,873 ops/us | |
ToString.IntPlusString -1000 thrpt 9 15,134 ± 3,197 ops/us | |
ToString.IntPlusString 200000 thrpt 9 12,207 ± 3,494 ops/us | |
ToString.IntPlusString 2147483647 thrpt 9 9,118 ± 1,187 ops/us | |
ToString.IntPlusString -2147483648 thrpt 9 23,403 ± 4,224 ops/us | |
ToString.IntegerToString 0 thrpt 9 20,160 ± 3,844 ops/us | |
ToString.IntegerToString 1 thrpt 9 18,452 ± 2,015 ops/us | |
ToString.IntegerToString -1000 thrpt 9 11,405 ± 0,831 ops/us | |
ToString.IntegerToString 200000 thrpt 9 8,810 ± 0,316 ops/us | |
ToString.IntegerToString 2147483647 thrpt 9 7,542 ± 0,624 ops/us | |
ToString.IntegerToString -2147483648 thrpt 9 75,560 ± 2,767 ops/us | |
ToString.StringBuilderAppend 0 thrpt 9 26,582 ± 3,667 ops/us | |
ToString.StringBuilderAppend 1 thrpt 9 27,113 ± 3,415 ops/us | |
ToString.StringBuilderAppend -1000 thrpt 9 14,386 ± 0,775 ops/us | |
ToString.StringBuilderAppend 200000 thrpt 9 11,085 ± 1,335 ops/us | |
ToString.StringBuilderAppend 2147483647 thrpt 9 7,125 ± 1,288 ops/us | |
ToString.StringBuilderAppend -2147483648 thrpt 9 22,065 ± 2,776 ops/us | |
ToString.StringPlusInt 0 thrpt 9 27,759 ± 2,075 ops/us | |
ToString.StringPlusInt 1 thrpt 9 25,794 ± 2,020 ops/us | |
ToString.StringPlusInt -1000 thrpt 9 15,098 ± 2,029 ops/us | |
ToString.StringPlusInt 200000 thrpt 9 11,955 ± 1,834 ops/us | |
ToString.StringPlusInt 2147483647 thrpt 9 6,938 ± 0,693 ops/us | |
ToString.StringPlusInt -2147483648 thrpt 9 21,517 ± 1,594 ops/us | |
ToString.StringValueOf 0 thrpt 9 16,575 ± 1,510 ops/us | |
ToString.StringValueOf 1 thrpt 9 17,559 ± 2,895 ops/us | |
ToString.StringValueOf -1000 thrpt 9 10,581 ± 0,856 ops/us | |
ToString.StringValueOf 200000 thrpt 9 9,488 ± 1,845 ops/us | |
ToString.StringValueOf 2147483647 thrpt 9 6,804 ± 0,256 ops/us | |
ToString.StringValueOf -2147483648 thrpt 9 74,555 ± 4,836 ops/us |
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
// class name and method names are not Java convention but readable | |
package net.eckenfels.jmhtest; | |
import java.util.concurrent.TimeUnit; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Threads; | |
import org.openjdk.jmh.annotations.Warmup; | |
// default benchmark settings (can be overwritten on command line) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
@Warmup(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS) | |
@Threads(value=1) | |
@Fork(value=3) | |
@State(Scope.Thread) | |
public class ToString | |
{ | |
// repeat all tests with the following integers | |
@Param(value = {"0", "1", "-1000", "200000", ""+Integer.MAX_VALUE, ""+Integer.MIN_VALUE}) | |
int n; | |
// JMH alloes you to return anything, this avoids optimizations | |
@Benchmark | |
public String StringValueOf() | |
{ | |
return String.valueOf(n); | |
} | |
@Benchmark | |
public String IntegerToString() | |
{ | |
return Integer.toString(n); | |
} | |
@Benchmark | |
public String StringBuilderAppend() | |
{ | |
return new StringBuilder().append(n).toString(); | |
} | |
@Benchmark | |
public String StringPlusInt() | |
{ | |
return "" + n; | |
} | |
@Benchmark | |
public String IntPlusString() | |
{ | |
return n + ""; | |
} | |
} |
Would you also test String.format()
? Just curious :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is german locale, so
,
is the decimal seperator. Results are in operations per micro second.