Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Effectiveness and code optimization in Java

763 views

Published on

-- What is effectiveness?
-- Code optimization
-- JVM optimization
-- Code samples
-- Measurements

Published in: Software
  • Be the first to comment

Effectiveness and code optimization in Java

  1. 1. Effectiveness and code optimization in Java Sergey Morenets, sergey.morenets@gmail.com December, 4 2015
  2. 2. About author • Works in IT since 2000 • 12 year of Java SE/EE experience • Regular speaker at Java conferences • Author of “Development of Java applications” and “Main errors in Java programming ”books • Founder of http://it-simulator.com
  3. 3. Preface
  4. 4. Agenda
  5. 5. Agenda • What is effectiveness? • Code optimization • JVM optimization • Code samples • Measurements
  6. 6. Ideal code Concise Readable Self-describing Reusable Testable Modern Flexible Scalable Effective
  7. 7. Effectiveness • Hard to determine on code/design review stages or in unit-tests • Is relevant for the specific project configuration • Cannot be defined in development environment • Depends on the application environment • Premature optimization is evil • Hardware-specific • The only aspect of the ideal code that affects users
  8. 8. Premature optimization
  9. 9. Premature optimization
  10. 10. Premature optimization
  11. 11. Premature optimization
  12. 12. Premature optimization
  13. 13. Effectiveness CPU utilization Memory utilization Network I/O utilization Disk I/O utilization
  14. 14. Effectiveness • Can be measured • Can be static or dynamic • Can be tuned
  15. 15. Tuning • JVM options • Metaspace/heap/stack size • Garbage collector options • http://blog.sokolenko.me/2014/11/javavm-options- production.html • http://www.javaspecialists.eu/
  16. 16. Code optimization Java compiler JIT compiler JVM
  17. 17. Code optimization
  18. 18. Code optimization public int execute(); Code: 0: iconst_2 1: istore_1 2: iinc 1, 1 5: iconst_1 6: ireturn
  19. 19. Code optimization
  20. 20. Code optimization public int execute(); Code: 0: iconst_2 1: istore_1 2: iconst_1 3: ireturn
  21. 21. Code optimization
  22. 22. Code optimization public void execute(); Code: 0: return
  23. 23. Code optimization
  24. 24. Code optimization public static boolean get(); Code: 0: iconst_1 1: ireturn
  25. 25. Code optimization
  26. 26. Code optimization public void execute(); Code: 0: return
  27. 27. Code optimization
  28. 28. Code optimization public int execute(); Code: 0: iconst_2 1: istore_1 2: iconst_4 3: istore_2 4: iload_1 5: iload_2 6: iadd 7: ireturn
  29. 29. Code optimization
  30. 30. Code optimization public int execute(); Code: 0: bipush 6 2: ireturn
  31. 31. Code optimization
  32. 32. Code optimization public int execute(); Code: 0: bipush 12 2: ireturn
  33. 33. Javacompiler Dead code elimination Constant folding Fixed expression calculation
  34. 34. Measurements • JMH is micro benchmarking framework • Developed by Oracle engineers • First release in 2013 • Requires build tool(Maven, Gradle) • Can measure throughput or average time • Includes warm-up period
  35. 35. Warm-up
  36. 36. Environment • JMH 1.11.1 • Maven 3.3.3 • JDK 1.8.0.65 • Intel Core i7, 4 cores, 16 GB
  37. 37. Measurements Type Time(ns) Multiply 4 2,025 Shift 2,024
  38. 38. Measurements Type Time(ns) Multiply 17 2,04 Shift 2,04
  39. 39. Sample
  40. 40. Method vs Field
  41. 41. Measurements Type Time(ns) Field 1,945 Method 1,933
  42. 42. Measurements Type Time(ns) Static 2,17 Instance 2,30
  43. 43. Increment
  44. 44. Measurements Type Time(ns) Addiction 2,06 Incrementation 2,06
  45. 45. Swap
  46. 46. Measurements Type Time(ns) Standard 2,10 Addiction 2,08 XOR 2,04
  47. 47. Conditions
  48. 48. Measurements Type Time(ns) Ternary operation 2,05 If operator 2,03
  49. 49. Parsing
  50. 50. Measurements Type Time(ns) Long usage(small numbers) 3 Long usage(average numbers) 19 Long usage(big numbers) 33 Long usage (not a number) 849 Regexp (small numbers) 58 Regexp (average numbers) 75 Regexp (big numbers) 82 Regexp (not a number) 47
  51. 51. Copying arrays
  52. 52. Measurements Type Time(ns) Loop(1000 elements) 52,6 arrayCopy(1000 elements) 45,6 Loop(100 000 elements) 21666 arrayCopy(100 000 elements) 21707 Loop(10 000 000 elements) 6432557 arrayCopy(10 000 000 elements) 6616976
  53. 53. Measurements Type Time(ns) Rows(10 elements) 37 Columns(10 elements) 67 Rows(100 elements) 2954 Columns(100 elements) 5567 Rows(1000 elements) 264340 Columns(1000 elements) 1300244 Rows(5000 elements) 9,6(ms) Columns(5000 elements) 387(ms)
  54. 54. Strings
  55. 55. Measurements Type Time(ns) + 7,62 Concat 13,4 StringBuffer 7,32 StringBuilder 7,24
  56. 56. Maps
  57. 57. Measurements Type Time(ns) Entries (10 pairs) 30 Keys/Values(10 pairs) 70 Entries (1000 pairs) 2793 Keys/Values(1000 pairs) 8798 Entries (200 000 pairs) 237652 Keys/Values(200 000 pairs) 350821
  58. 58. Arrays
  59. 59. Measurements Type Time(ns) For (10 elements) 4,9 For-each (10 elements) 5,1 For (1000 elements) 260 For-each (1000 elements) 259,9 For (50000 elements) 12957 For-each (50000 elements) 12958
  60. 60. Arrays
  61. 61. Measurements Type Time(ns) For (10 elements) 5,04 For optimized(10 elements) 5,07 For (1000 elements) 258,9 For-each (1000 elements) 258,7
  62. 62. Arrays
  63. 63. Measurements Type Time(ns) Sequential (10 elements) 5 Parallel (10 elements) 6230 Sequential (1000 elements) 263 Parallel (1000 elements) 8688 Sequential (50000 elements) 13115 Parallel (50000 elements) 34695
  64. 64. Measurements Type Time(ns) Sequential (10 elements) 5 Parallel (10 elements) 6230 Sequential (1000 elements) 263 Parallel (1000 elements) 8688 Sequential (50000 elements) 13115 Parallel (50000 elements) 34695 Sequential (5 000 000 elements) 1 765 206 Parallel (5 000 000 elements) 2 668 564 Sequential (500 000 000) 183 ms Parallel (500 000 000) 174 ms
  65. 65. Exceptions
  66. 66. Measurements Type Time(ns) If 2,40 Exceptions 2,47
  67. 67. Strings
  68. 68. Strings
  69. 69. Measurements Type Time(ns) Parse(1000 tokens) 12239 Parse with intern(1000 tokens) 72814
  70. 70. Arrays
  71. 71. Bitset
  72. 72. Measurements Type Time(ns) Fill array (1000 elements) 148 Fill bit set(1000 elements) 1520 Fill array (50 000 elements) 4669 Fill bit set(50 000 elements) 71395
  73. 73. Bitset
  74. 74. Measurements Type Time(ns) Fill array (1000 elements) 148 Fill bit set(1000 elements) 261 Fill array (50 000 elements) 4669 Fill bit set(50 000 elements) 12758
  75. 75. Collections
  76. 76. Collections
  77. 77. Measurements Type Time(ns) New ArrayList() 3,6 emptyList() 2,4
  78. 78. Autoboxing
  79. 79. Measurements Type Time(ns) Array(100 elements) 58 List(100 elements) 390 Array(10 000 elements) 4776 List(10 000 elements) 48449
  80. 80. LIFO
  81. 81. LIFO Stack LinkedList ArrayDeque
  82. 82. Stack
  83. 83. Measurements Type Time(ns) Stack(1000 elements) 5729 LinkedList(1000 elements) 7928 ArrayDeque(1000 elements) 4366 Stack(100 000 elements) 588770 LinkedList(100 000 elements) 773684 ArrayDeque(100 000 elements) 484280
  84. 84. Collections
  85. 85. Collections Type Time(ns) Fill HashMap(1000 elements) 16000 Fill TreeMap(1000 elements) 40115 Fill HashMap(100 000 elements) 2 027 116 Fill TreeMap(100 000 elements) 11 195 422 Iteration HashMap(1000 elements) 3086 Iteration TreeMap(1000 elements) 5038
  86. 86. Reflection
  87. 87. Reflection Type Time(ns) instanceof 2,266 isAssignableFrom 2,146 == 2,401
  88. 88. Reflection
  89. 89. Measurements Type Time(ns) New object 3,0 Reflection 5,4
  90. 90. Reflection
  91. 91. Measurements Type Time(ns) Method call 0,3 Reflection 232
  92. 92. Measurements Type Time(ns) Method call 0,3 Reflection (cached) 3,1
  93. 93. Cloning
  94. 94. Measurements Type Time(ns) Clone 6,5 Direct copying 3,8
  95. 95. Sample
  96. 96. Lists
  97. 97. Measurements Type Time(ns) ArrayList (1000 elements) 4766 ArrayList (100 000 elements) 381707 LinkedList (1000 elements) 5504 LinkedList (100 000 elements) 504231
  98. 98. JIT optimization • Inline methods • Eliminate locks • Replace interface with direct method calls • Join synchronized blocks • Eliminate dead code • Drop memory write for non-volatile variables
  99. 99. Theory
  100. 100. Q&A • Sergey Morenets, sergey.morenets@gmail.com

×