#' Elapsed Time
#'
#' This program calculates the elapsed time between concecutive calls.
#'
#' @param opt = 0 to reset the clock \cr
#' = 1  to print the elapsed time. \cr
#' = 2  to print the elapsed time and reset the clock.
#' @param title a title string
#'
#' @details
#' A global variable ..start.. is used.
#'
#' @return
#' etime in seconds.
#'
#' @examples
#' etime(0)
#' x=rnorm(10000000)
#' etime(1)
#' x=rnorm(10000000)
#' etime(2)
#' x=rnorm(10000000)
#' etime(1, "This is the title string.")
#' rm(x)
#'
#' @export
#'

etime <- function( opt=0, title=NULL ){
   ## elapsed time
   ## Shin-ichi Mayekwa: 070702,03
   ## opt=2: 20150615
   ## title: 20240331
   ##
   ## etime(0) resets the clock.
   ## etime()  prints and returns the elapsed time.
   ##
   ## ..start..<-numeric(0)
   if( opt == 0 ) ..start.. <<- Sys.time()
   else if( opt >= 1 ){
      if( !is.null(title) ) cat("\n", title, "\n", sep="")
      cat( "started at ", ..start.., " " )
      print( ..start.. )
      endtime=Sys.time()
      cat( "  ended at ", endtime, " " )
      print( endtime )
      etime=unclass(endtime)-unclass(..start..)
      # cat( "elapsed time = ", etime, "seconds\n" )
      h=trunc(etime/3600); m=trunc((etime%%3600)/60); s=(etime-3600*h-60*m);
      cat( "elapsed time as H:M:S = ", h, ":", m, ":", s, "\n" )
      if( opt == 2 ) ..start.. <<- Sys.time()
      return(etime)
   }
} # end of etime


