New to R programming ... a question regarding microbenchmark

I'm working on an assignment and have installed the library(microbenchmark).
I would appreciate a quick response so I can hopefully streamline the work to collect the execution times and complete the required programming tasks...

First, I setup the utility to generate execution time comparisons for two different functions.
The code works and I receive the output from the program:
code:

benchmark.height <- microbenchmark(**heart.disease.b** = within(heart.disease.b, height<- 
                                 height/100), **heart.disease.t** = mutate(heart.disease.b, 
                                 height=height/100),
                                 unit='s', times=10)

Output:

print(benchmark.height, signif = 2)
Unit: seconds
            expr     min      lq    mean  median      uq     max neval
 heart.disease.b 0.00020 0.00020 0.00023 0.00021 0.00022 0.00035    10
 heart.disease.t 0.00019 0.00019 0.00027 0.00020 0.00027 0.00084    10
```````````
however, when I try to display the dataframe heart.disease.b, I see the arithmetic was not performed on the indicated column= height to convert it from cm to meters.
Also, the second dataframe = heart .disease.t has not been created..., I receive a "object not found error:
```
Error in head(heart.disease.t) : object 'heart.disease.t' not found
```
So, is it true that microbenchmark is only analyzing the timing of the functions?

Is there a way to tell microbenchmark to do the timing analysis and actually execute the functions?

Or can I only collect the execution times via microbenchmark and then must run the code separately to accomplish the programming tasks?

Thank you in advance for any guidance.

John2

I'll start by explaining the major confusion here.
With microbenchmark, its expecting a list of expressions, and you can name these, which is what you've done. However because its treated like that, you are not doing any assignments with the '=' (at least not in the sense of assigning to global environment. For assignment use <- to avoid ambiquity.

Here is an example with iris

library(microbenchmark)
library(tidyverse)
benchmark_iris <- microbenchmark(
  res_a_label_only = res_a <- within(iris, Petal.Length <-
                             Petal.Length / 100), 
  res_b_label_only = res_b <-  mutate(iris,
                Petal.Length =  Petal.Length / 100
  ),
  unit = "s", times = 10
)

head(res_a)
head(res_b)
identical(res_a,res_b)
benchmark_iris

the ** in your code confused me, where they for emphasis of some kind ?

1 Like

The ** were selected to make that word bold type, but it did not work.

I was using the equal sign as that is the syntax I found.
I'll try switch to the assignment.

Thank you.

John

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.