src/Pure/ML/ml_profiling.scala
changeset 75393 87ebf5a50283
parent 73843 014b944f4972
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
    10 import java.util.Locale
    10 import java.util.Locale
    11 
    11 
    12 import scala.collection.immutable.SortedMap
    12 import scala.collection.immutable.SortedMap
    13 
    13 
    14 
    14 
    15 object ML_Profiling
    15 object ML_Profiling {
    16 {
    16   sealed case class Entry(name: String, count: Long) {
    17   sealed case class Entry(name: String, count: Long)
       
    18   {
       
    19     def clean_name: Entry = copy(name = """-?\(\d+\).*$""".r.replaceAllIn(name, ""))
    17     def clean_name: Entry = copy(name = """-?\(\d+\).*$""".r.replaceAllIn(name, ""))
    20 
    18 
    21     def print: String =
    19     def print: String =
    22       String.format(Locale.ROOT, "%12d %s",
    20       String.format(Locale.ROOT, "%12d %s",
    23         count.asInstanceOf[AnyRef], name.asInstanceOf[AnyRef])
    21         count.asInstanceOf[AnyRef], name.asInstanceOf[AnyRef])
    24   }
    22   }
    25 
    23 
    26   sealed case class Report(kind: String, entries: List[Entry])
    24   sealed case class Report(kind: String, entries: List[Entry]) {
    27   {
       
    28     def clean_name: Report = copy(entries = entries.map(_.clean_name))
    25     def clean_name: Report = copy(entries = entries.map(_.clean_name))
    29 
    26 
    30     def total: Entry = Entry("TOTAL", entries.iterator.map(_.count).sum)
    27     def total: Entry = Entry("TOTAL", entries.iterator.map(_.count).sum)
    31 
    28 
    32     def print: String =
    29     def print: String =
    33       ("profile_" + kind + ":\n") + cat_lines((entries ::: List(total)).map(_.print))
    30       ("profile_" + kind + ":\n") + cat_lines((entries ::: List(total)).map(_.print))
    34   }
    31   }
    35 
    32 
    36   def account(reports: List[Report]): List[Report] =
    33   def account(reports: List[Report]): List[Report] = {
    37   {
       
    38     val empty = SortedMap.empty[String, Long].withDefaultValue(0L)
    34     val empty = SortedMap.empty[String, Long].withDefaultValue(0L)
    39     var results = SortedMap.empty[String, SortedMap[String, Long]].withDefaultValue(empty)
    35     var results = SortedMap.empty[String, SortedMap[String, Long]].withDefaultValue(empty)
    40     for (report <- reports) {
    36     for (report <- reports) {
    41       val kind = report.kind
    37       val kind = report.kind
    42       val map = report.entries.foldLeft(results(kind))(
    38       val map = report.entries.foldLeft(results(kind))(