niklas-heer / speed-comparison

A repo which compares the speed of different programming languages.
https://niklas-heer.github.io/speed-comparison
MIT License
475 stars 76 forks source link

R improvement #44

Closed jonocarroll closed 1 year ago

jonocarroll commented 1 year ago

Can I suggest a ~5x speedup for R (on my machine - the ~7s in the comparison seems a lot) leveraging vectorization?

system.time({
  fileName <- "rounds.txt"
  rounds <- as.numeric(readChar(fileName, file.info(fileName)$size))

  x = 1.0
  pi = 1.0

  for (i in 2:(rounds + 2)){
    x = x * -1
    pi = pi + (x / (2 * i - 1))
  }

  pi = pi * 4

  printf <- function(...) cat(sprintf(...))
  printf("%.16f\n", pi)
})
#> 3.1415926435893260
#>    user  system elapsed 
#>   2.413   0.006   2.420

system.time({
  fileName <- "rounds.txt"
  rounds <- as.numeric(readChar(fileName, file.info(fileName)$size))

  pi <- 4*(1+sum(c(-1,1)/((seq.int(4, 2*rounds+2, 2)-1))))

  printf <- function(...) cat(sprintf(...))
  printf("%.16f\n", pi)
})
#> 3.1415926635897682
#>    user  system elapsed 
#>   0.380   0.084   0.476

Created on 2022-10-17 by the reprex package (v2.0.1)

niklas-heer commented 1 year ago

@jonocarroll that sounds very promising. Thank you! Are you willing to create a PR?

jonocarroll commented 1 year ago

Yep, can do. I'll also try to run the whole suite to run a fair comparison.

niklas-heer commented 1 year ago

@jonocarroll that is great 👍

I'll also try to run the whole suite to run a fair comparison.

You can do it, but would also be fine if you just run R to make sure it works and leave running the whole affair to the CI in your PR :)

Meaning this would be enough: earthly --config earthly-config.yml +R

EDIT: The CI run produces a .zip file (combined-results) with all the results in it. (see this for reference)

niklas-heer commented 1 year ago

Closed by #45.