# stringi benchmark results #

```{r,MainSettings,echo=FALSE,cache=FALSE,warning=FALSE,message=FALSE}


benchmarks_wildcard <- '*'


options(encoding="utf-8")
# options(width=60)
require("knitr")
# options(digits=7)

require("tikzDevice")

opts_knit$set(progress = FALSE, verbose = FALSE)

opts_chunk$set(
   keep.source=TRUE,
   fig.path='figure/rbmark/',
#    cache.path='cache-knitr-mbmark/',
   cache=FALSE,
   tidy=TRUE,
   prompt=TRUE,
   dev='png',
   external=TRUE
#    fig.align='center',
#    size='footnotesize'
)
```


```{r,results='asis',echo=FALSE,message=FALSE,fig.height=6,fig.width=12}
require('RColorBrewer')
require('stringi')
indir <- "." # "devel/benchmarks"

alltests <- c()
resdirs <- dir(indir, glob2rx("results-rbmark_"%s+%benchmarks_wildcard), full.names=TRUE)
for (d in resdirs) {
   alltests <- c(alltests, dir(d, glob2rx("*.csv"), full.names=TRUE))
}

alltests <- unique(basename(alltests))


for (test_i in seq_along(alltests)) {
   
   test <- alltests[test_i]
   results_info <- lapply(resdirs, function(d) {
      if (!file.exists(file.path(d, test))) return(NULL)
      raw <- readLines(file.path(d, test))
      raw <- raw[stri_detect_regex(raw, "^#")]
      raw <- stri_replace_all_regex(raw, "^# *", "")
      raw <- stri_match_first_regex(raw, '^([^=]+)=(.*)$')
      res <- raw[,3]
      names(res) <- raw[,2]
      c(res, 'ident'=stri_extract_last_regex(d, '[^_]+'))
   })
   
   results_data <- lapply(resdirs, function(d) {
      if (!file.exists(file.path(d, test))) return(NULL)
      data <- read.csv(file.path(d, test), comment='#')      
      data$datetime <- strptime(data$datetime, "%Y-%m-%d %H:%M:%S")
      data$time <- (data$user.self + data$sys.self)/data$n
      data
   })
   
   if (test_i == 1) {
      cat('## Machine info ##\n\n')
      
      for (info in results_info) {
         cat('data | value\n')
         cat('---- | -----\n')
         cat(paste(names(info[-(1:2)]), ' | ', info[-(1:2)]), sep='\n')
         cat('\n----------------------------\n\n')
      }
   }

   cat('## '%s+%results_info[[1]]['benchmark']%s+%' ##\n\n')
   cat(' > '%s+%results_info[[1]]['description']%s+%'\n\n')
   
   min.time <- min(sapply(results_data, function(data) min(data$time)))
   max.time <- max(sapply(results_data, function(data) max(data$time)))
   rngy <- c(min.time, max.time)
#    rngy <- rngy + c(-1,1)*diff(rngy)*0.05
   
   
   for (i in seq_along(results_data)) {
      
      data <- results_data[[i]]
      info <- results_info[[i]]
      if (is.null(data)) next
      
      ntests <- nlevels(data$expr)
      nmeasurements <- max(table(data$expr))
      idxrepresent <- which.max(table(data$expr))
      
      ## Setup figure
      par(xaxt='n')
      plot(1:nmeasurements,
         rep(NA, nmeasurements), ylim=rngy,
         xlab='Measurement', ylab='Time [s]',
         main=info['benchmark']%s+%' '%s+%info['ident']%s+%' '%s+%info['LC_CTYPE'])
      par(xaxt='s')
      axis(side=1, at=1:nmeasurements)
      abline(h=axTicks(2), col='gray', lty=3)
      abline(v=1:nmeasurements, col='gray', lty=3)
      
      # setup pchs and col
      pch <- rep(16, ntests)
      col <- rep(0, ntests)
      n_stringi <- 0
      n_stringr <- 0
      n_other <- 0
      for (i in 1:ntests) {
         if (stri_detect_fixed(levels(data$expr)[i], "stri_") ||
             stri_detect_regex(levels(data$expr)[i], "%\\P{Z}+?%")) {
            pch[i] <- 15
            col[i] <- 4+n_stringi
            n_stringi <- n_stringi + 1
         }
         else if (stri_detect_fixed(levels(data$expr)[i], "str_")) {
            pch[i] <- 17
            col[i] <- 7+n_stringr
            n_stringr <- n_stringr + 1
         }
         else {
            col[i] <- 1+n_other
            n_other <- n_other + 1
         }
      }
   
      col <- c("#ef6548", "#990000", "#fdd49e",
               "#084594", "#4292c6", "#9ecae1",
               "#41ae76", "#ffffcc", "#005824")[col]
      
      git_sha <- data$git_sha[data$expr == data$expr[idxrepresent]]
      datetime <- as.character(data$datetime)[data$expr == data$expr[idxrepresent]]
      for (i in 1:nmeasurements) {
         text(i, mean(rngy), sprintf("%s (%s)", git_sha[i], datetime[i]),
               srt=90, col='gray')
      }
      
      measurements <- unique(data$datetime)
      for (i in 1:ntests) {
         subdata <- data[data$expr == levels(data$expr)[i],]
         xmeasurements <- match(subdata$datetime, measurements)
         
         
         
         matlines(xmeasurements, subdata$time,
            col=col[i], lwd=c(3), type='l', lty=c(1))
         matpoints(xmeasurements,
            subdata$time,
            pch=pch[i], col=col[i], cex=2)
      }
      legend('topleft', levels(data$expr), col=col, bg='#ffffffc0',
         lty=1, lwd=2, pch=pch)
      
      perc <- unique(pmax(1,pretty(c(min.time, max.time)/min.time)))
      abline(h=perc*min.time, lty=3, col=3)
      axis(4, at=perc*min.time, labels=sprintf("%gx", perc))
      
      cat('\n')
      dev.flush()
      cat('\n')   
   }
}
```
