I was wondering how much faster a!=0
is than !a==0
and used the R package microbenchmark.
Here's the code (reduce 3e6 and 100 if your pc is slow):
library("microbenchmark")
a <- sample(0:1, size=3e6, replace=TRUE)
speed <- microbenchmark(a != 0, ! a == 0, times=100)
boxplot(speed, notch=TRUE, unit="ms", log=F)
Everytime, I get a plot like the one below. As expected, the first version is faster (median 26 milliseconds) than the second (33 ms).
But where do these few very high values (outliers) come from? Is that some memory management effect? If I set times to 10, there are no outliers...
Edit: sessionInfo(): R version 3.1.2 (2014-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit)
times=10
or so. Keep in mind thatmicrobenchmark
is not bulletproof. There are a couple blogs somewhere pointing out semibugs in how it collects timing info. It may also be simply that some other "thing" happens every now and then during the normal course ofR
operations - agc
call, or waiting for RAM reallocation at the system level, etc. Perhaps try running a loop aroundsystem.time
to see what the distribution of results is? – Pectize