Last active
March 4, 2018 13:29
-
-
Save GarrettMooney/5c4b477a4abf27d5ef5150038b1c25ac to your computer and use it in GitHub Desktop.
fibonacci comparison of rust vs. compiled R vs. R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compiler::enableJIT(0) | |
compiler::enableJIT(0) | |
rustinr::rust(code = ' | |
// #[rustr_export] | |
pub fn fib_rust(a : i32) -> RResult<f64>{ | |
let mut first = 0.0; | |
let mut second = 1.0; | |
let mut third = 0.0; | |
for _ in 0..a { | |
third = first + second; | |
first = second; | |
second = third; | |
} | |
Ok(first) | |
} | |
') | |
fib_r <- function(n) { | |
first <- 0 | |
second <- 1 | |
third <- 0 | |
for (i in seq_len(n)) { | |
third <- first + second | |
first <- second | |
second <- third | |
} | |
first | |
} | |
fib_r_cmp <- compiler::cmpfun(fib_r) | |
fib_rust <- compiler::cmpfun(fib_rust) | |
stopifnot(fib_r(1000L) == fib_r_cmp(1000L)) | |
stopifnot(fib_r(1000L) == fib_rust(1000L)) | |
microbenchmark::microbenchmark( | |
'r' = fib_r( 1000L), | |
'r_compiled' = fib_r_cmp(1000L), | |
'rust' = fib_rust( 1000L) | |
) | |
## Unit: microseconds | |
## expr min lq mean median uq max neval cld | |
## r 229.796 231.2595 240.78482 232.9030 237.7065 340.625 100 c | |
## r_compiled 46.829 48.0710 52.06551 49.1385 52.6400 106.518 100 b | |
## rust 1.762 1.9325 2.42770 2.2695 2.4615 14.861 100 a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment