# R-alpha: Single precision -- why not ? // source for unix.time(.)

Martin Maechler (maechler@stat.math.ethz.ch)
Wed, 3 Apr 96 09:22:43 +0200

```Date: Wed, 3 Apr 96 09:22:43 +0200
Message-Id: <9604030722.AA17167@>
From: Martin Maechler <maechler@stat.math.ethz.ch>
To: R-testers@stat.math.ethz.ch
Subject: R-alpha: Single precision -- why not ? // source for  unix.time(.)

Following a few thoughts ..

What about  single precision numbers & arithmetic?
~~~~~~~~~~~~~~~~
Did you leave it away in order to save your programming time, only,
or is there more to it ?

A good approach to single precision arithmetic would use SP arithmetic
on SP numbers automatically,
[I suppose this is what happens with integer arithmetic currently,
or not ?]
and with large  matrices / vectors could save considerable time/memory,
(multiplying two doubles, each 2 'words' of 32 bits,
sounds a factor of 4 'harder' than multiplying two singles, each 1word).

or do you think this will not be a point once that "all" computers will
be  64-bit ? [SP number = 32 bit;  DP number = 64 bit]?

---
Here, I post some results on  'integer' vs. 'double'  [from a Sun SPARC 5],
including the source of  unix.time(.)
[which of course only works where  proc.time() works]:

unix.time <- function(expr)
{
## Purpose: Return CPU (and other) times that 'expr' used ..
##	Modelled after S`s  "unix.time"; to be used with R (rel. 0.4 & later)
## -------------------------------------------------------------------------
## Arguments: expr: 'any' valid R expression
## -------------------------------------------------------------------------
## Author: Martin Maechler, Date:  2 Apr 96
## -------------------------------------------------------------------------
loc.frame <- sys.parent(1)
##-Dbg   cat("loc.frame <- sys.parent(1) ="); print(loc.frame)
##-Dbg   cat("\tmode=",mode(loc.frame),"\n")

##-S   if(loc.frame == 1)
##-S    loc.frame <- F
on.exit(cat("Timing stopped at:", proc.time() - time, "\n"))
expr <- substitute(expr)
time <- proc.time()
eval(expr, envir = loc.frame) #<-- 'R'
new.time <- proc.time()
on.exit()
if(length(new.time) == 3)    new.time <- c(new.time, 0, 0)
if(length(time) == 3)        time     <- c(time, 0, 0)
new.time - time
}

ii _ as.integer(1:10000)
rii _ as.double(ii)

typeof(ii * ii) #->  integer
typeof(rii * rii)#-> real

Ti _ vector("real", length = 10)
Tr _ vector("real", length = 10)
for(n in seq(10)) {
Ti[n] _ unix.time(for(i in 1:100) x <-  ii *  ii)[1]
Tr[n] _ unix.time(for(i in 1:100) x <- rii * rii)[1]
}
## CPU Times for Sun SPARC 5 (110 MHz)
##              min   25%   50%   75%   max
cat("   real: ");round(fivenum(Tr)) #>   real: [1] 14100 15120 15330 15480 15720
cat("integer: ");round(fivenum(Ti)) #> nteger: [1]  7740  7860  7980  9120  9240
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
```