{"id":5587,"date":"2018-01-03T00:44:01","date_gmt":"2018-01-03T00:44:01","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5587"},"modified":"2018-01-03T00:44:01","modified_gmt":"2018-01-03T00:44:01","slug":"time-method-print-memory-use-scala","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/time-method-print-memory-use-scala\/","title":{"rendered":"Time method and print memory use in Scala"},"content":{"rendered":"<p>The following code will print out how long a method takes, return it&#8217;s value, and print out how much memory it consumed.<\/p>\n<p>Example:<\/p>\n<pre>\n[info] Elapsed time (solr): 1.207055231s (solr)\n[info] Memory: (solr): -33MB\n[info] Elapsed time (tokenize): 1.091507079s (tokenize)\n[info] Memory: (tokenize): -50MB\n[info] Elapsed time (df): 3.809766467s (df)\n[info] Memory: (df): -38MB\n[info] Elapsed time (vectors): 19.631056498s (vectors)\n[info] Memory: (vectors): -698MB\n<\/pre>\n<pre lang=\"scala\">\n  val runtime = Runtime.getRuntime()\n  val format = NumberFormat.getInstance()\n\n  def time[R](name: String, block: => R): R = {\n    val maxMemory1 = runtime.maxMemory()\n    val allocatedMemory1 = runtime.totalMemory()\n    val freeMemory1 = runtime.freeMemory()\n    val totalFree1 = freeMemory1 + (maxMemory1 - allocatedMemory1)\n\n    val t0 = System.nanoTime()\n    val result = block\n    val t1 = System.nanoTime()\n\n    val maxMemory2 = runtime.maxMemory()\n    val allocatedMemory2 = runtime.totalMemory()\n    val freeMemory2 = runtime.freeMemory()\n    val totalFree2 = freeMemory2 + (maxMemory2 - allocatedMemory2)\n\n    println(\n      \"Elapsed time (\" + name + \"): \" + (t1 - t0) \/ 1000000000.0 + \"s (\" + name + \")\" + \"\\n\" +\n      \"Memory: (\" + name + \"): \" + format.format((totalFree2 - totalFree1) \/ 1024 \/ 1024) + \"MB\"\n    )\n\n    result\n  }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Print out method timing + memory use in scala<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5587"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5587"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5587\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}