Skip to content

Instantly share code, notes, and snippets.

@HenrikBengtsson
Created June 13, 2015 22:48
Show Gist options
  • Save HenrikBengtsson/70776e3867cbe185a381 to your computer and use it in GitHub Desktop.
Save HenrikBengtsson/70776e3867cbe185a381 to your computer and use it in GitHub Desktop.

The overhead of method dispatching can be noticable, particularly if done on small objects and it adds up if doing many many times. For instance,

> add <- function(a,b) UseMethod("add")
> add.integer <- function(a,b) a+b

gives with method dispatching:

> system.time({ for (kk in 1:10e6) add(1L, 2L) })
  user  system elapsed
 35.43    0.05   35.70

and without:

> system.time({ for (kk in 1:10e6) add.integer(1L, 2L) })
  user  system elapsed
  7.80    0.02    7.86
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment