Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. package jmh;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. import org.openjdk.jmh.annotations.Benchmark;
  9. import org.openjdk.jmh.annotations.BenchmarkMode;
  10. import org.openjdk.jmh.annotations.Level;
  11. import org.openjdk.jmh.annotations.Mode;
  12. import org.openjdk.jmh.annotations.OutputTimeUnit;
  13. import org.openjdk.jmh.annotations.Param;
  14. import org.openjdk.jmh.annotations.Scope;
  15. import org.openjdk.jmh.annotations.Setup;
  16. import org.openjdk.jmh.annotations.State;
  17. import org.openjdk.jmh.annotations.TearDown;
  18. import org.openjdk.jmh.runner.Runner;
  19. import org.openjdk.jmh.runner.RunnerException;
  20. import org.openjdk.jmh.runner.options.Options;
  21. import org.openjdk.jmh.runner.options.OptionsBuilder;
  22.  
  23. /**
  24. * @author alexlovkov
  25. */
  26. public class StringTableBenchmark {
  27.  
  28. public static void main(String[] args) throws RunnerException {
  29. Options opt = new OptionsBuilder()
  30. .warmupIterations(5)
  31. .measurementIterations(15)
  32. .threads(1)
  33. .forks(1)
  34. .build();
  35. new Runner(opt).run();
  36. }
  37.  
  38. @Benchmark
  39. @BenchmarkMode(Mode.AverageTime)
  40. @OutputTimeUnit(TimeUnit.MILLISECONDS)
  41. public int benchmark(BenchmarkState state) {
  42. System.gc();
  43. return state.list.size();
  44. }
  45.  
  46. @State(Scope.Benchmark)
  47. public static class BenchmarkState {
  48.  
  49. @Param({"10000", "100000", "1000000"})
  50. int tableSize;
  51.  
  52. List<String> list;
  53.  
  54. @Setup(Level.Trial)
  55. public void init() {
  56. Random random = new Random();
  57. list = new ArrayList<>();
  58. for (int i = 0; i < tableSize; i++) {
  59. int length = 10;
  60. StringBuilder sb = new StringBuilder();
  61. for (int j = 0; j < length; j++) {
  62. char c = (char) (random.nextInt(26) + 'a');
  63. sb.append(c);
  64. }
  65. String s = sb.toString().intern();
  66. list.add(s);
  67. }
  68. }
  69. }
  70. }
  71.  
  72.  
  73. # Run complete. Total time: 00:01:30
  74.  
  75. Benchmark (tableSize) Mode Cnt Score Error Units
  76. StringTableBenchmark.benchmark 10000 avgt 15 6.913 ± 1.062 ms/op
  77. StringTableBenchmark.benchmark 100000 avgt 15 24.540 ± 1.233 ms/op
  78. StringTableBenchmark.benchmark 1000000 avgt 15 412.434 ± 14.148 ms/op
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement