Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1.  
  2. 2.4) 2) I get 115001, which i expect is an index error and that i will survive without the precision.
  3. real 0m15.719s
  4. user 0m0.015s
  5. sys 0m0.000s
  6. 3)
  7. class Memoizer2
  8. count: 615646
  9.  
  10. real 0m20.986s
  11. user 0m0.000s
  12. sys 0m0.000s
  13. We get such a high count since there is a very high chance of duplicate work, since the result is only
  14. put in the cache after we have computed the result. In this case, it's much slower than synchronized actually,
  15. but almost all of the threads does the expensive computation, which the synchronization method is relieved off, so that is why its slower.
  16.  
  17.  
  18. 4) class Memoizer3
  19. count: 132918
  20.  
  21. real 0m17.939s
  22. user 0m0.015s
  23. sys 0m0.000s
  24.  
  25. In this case we have much better results, reducing the need to call f by a factor of 10. However,
  26. unlucky timing will still result in that some threads will do the same work twice. For this solution
  27. to work, we need atomicity.
  28. 5)
  29.  
  30. class Memoizer4
  31. 115001
  32.  
  33. real 0m17.586s
  34. user 0m0.015s
  35. sys 0m0.000s
  36.  
  37. With Memoizer 4, i would have expected that we got under the synchronized method, and i'm not sure why it is not the case.
  38. However, what we see from the number of counts, is that we are now eliminating double computations, such that the computation will only be done once.
  39. This is with the use of atomicity that we lacked in Memoizer 3. It is still now rarely possible for two futures to be created wastefully, but
  40. the computation is the expensive part, not the memory.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement