Guest User

Untitled

a guest
Apr 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.io._
  2. import java.nio._
  3.  
  4. val f = new File("/dev/zero")
  5.  
  6. val size = 10000
  7. val count = 100000
  8.  
  9. def readUsingInputStream() {
  10. val is = new FileInputStream(f)
  11. val b = new Array[Byte](size)
  12. for (i <- 1 to count) {
  13. val c = is.read(b)
  14. if (c != size)
  15. throw new Exception()
  16. }
  17. is.close()
  18. }
  19.  
  20. def readUsingByteChannel() {
  21. val is = new FileInputStream(f)
  22. val c = is.getChannel
  23. val b = ByteBuffer.allocateDirect(size)
  24. for (i <- 1 to count) {
  25. c.read(b)
  26. if (b.position != size)
  27. throw new Exception()
  28. b.rewind()
  29. }
  30. is.close()
  31. }
  32.  
  33. def profile(what: String)(action: => Unit) = {
  34. val start = System.currentTimeMillis()
  35. action
  36. val delta = System.currentTimeMillis() - start
  37. println(what + ": " + delta)
  38. }
  39.  
  40. while (true) {
  41. profile("readUsingByteChannel")(readUsingByteChannel())
  42. profile("readUsingInputStream")(readUsingInputStream())
  43. }
  44.  
  45. println("$")
  46.  
  47. // vim: set ts=4 sw=4 et:
  48.  
  49. /*
  50.  
  51. outputs:
  52.  
  53. readUsingByteChannel: 132
  54. readUsingInputStream: 254
  55. readUsingByteChannel: 144
  56. readUsingInputStream: 253
  57. readUsingByteChannel: 133
  58. readUsingInputStream: 251
  59. readUsingByteChannel: 133
  60. readUsingInputStream: 251
  61. readUsingByteChannel: 132
  62. readUsingInputStream: 300
  63. readUsingByteChannel: 132
  64. readUsingInputStream: 254
  65.  
  66. */
Add Comment
Please, Sign In to add comment