English, British subtitles

← Improve Your Code To Reduce Churn

04-15 Improve Your Code To Reduce Churn

Get Embed Code
20 Languages

Showing Revision 1 created 07/31/2017 by Kerri Worthington.

  1. We can make a small adjustment in
    our code to prevent excessive churn.
  2. Let's take a look at a comparison view.
  3. Rather than concatenate one cell value
    at a time to build each row, let's use
  4. a StringBuilder instance, and construct
    each row using a single string.
  5. Note that the StringBuilder's
    instantiated outside the loop,
  6. and thus its memory is allocated once.
  7. And then we simply use
    it as a buffer for
  8. each iteration of the loop where we
    first clear it and then we append
  9. a single string of ints to represent
    the row for that loop iteration.
  10. Now see the instructor notes for
    more details into this code segment.
  11. All right, now it's time to verify.
  12. You want to go ahead and
    load the improved branch of code,
  13. which is called memory_churn_optimized,
    into both trace view and memory monitor
  14. to confirm we've reduced the amount of
    GC's occurring in the short time window.
  15. You may also use allocation
    tracker to verify.
  16. If you use allocation tracker,
  17. or if you got something unexpected
    in trace view, or memory monitor,
  18. share a screenshot of your
    output in the discussion forums.
  19. We're interested in
    seeing what you've got.
  20. Now for us, even with these changes,
    the Perf pirate still pauses.
  21. But this time for less time.
  22. Now at this point, this also might mean
    that this function is probably a good
  23. candidate to be backgrounded.