Re: [Rd] Converting an environment to a list: mget vs. as.list

2013-04-22 Thread Hadley Wickham
> I'd say the more interesting part is that ls() is so "slow" if you use the 
> ambiguous "name" argument instead of the direct "envir" one:
>
>> microbenchmark(ls(env), ls(envir=env), .Internal(ls(env, FALSE)))
> Unit: nanoseconds
>   expr   minlq medianuq   max neval
>ls(env) 12445 13422  14450 15144 37505   100
>ls(envir = env)  1741  2020   2331  2643 15574   100
>  .Internal(ls(env, FALSE))   631   730828   910  4157   100

That is interesting!

> Note that your objects are so small that you cannot distinguish constant cost 
> (e.g. just the method dispatch on as.list) - and that is in fact what causes 
> the difference - not the actual conversion:

That's a good point, but the first example was quite a bit larger, and
hence the impact of S3 dispatch slightly less:

env <- environment(plot)
names <- ls(env)

microbenchmark(
  as.list(env),
  as.list.environment(env),
  mget(names, env))

 expr min lq median uq max neval
 as.list(env)  42 49 51 53 140   100
 as.list.environment(env)  39 44 46 48 117   100
 mget(names, env)  33 35 37 38  83   100

so mget still wins in that case.

Hadley

--
Chief Scientist, RStudio
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Converting an environment to a list: mget vs. as.list

2013-04-22 Thread Simon Urbanek
On Apr 22, 2013, at 9:14 AM, Hadley Wickham wrote:

> Hi all,
> 
> An interesting discovery: if you want to convert an environment to a
> list, and you already know the names of the objects in the
> environment, using mget is about twice as fast as using as.list on the
> environment.
> 

I'd say the more interesting part is that ls() is so "slow" if you use the 
ambiguous "name" argument instead of the direct "envir" one:

> microbenchmark(ls(env), ls(envir=env), .Internal(ls(env, FALSE)))
Unit: nanoseconds
  expr   minlq medianuq   max neval
   ls(env) 12445 13422  14450 15144 37505   100
   ls(envir = env)  1741  2020   2331  2643 15574   100
 .Internal(ls(env, FALSE))   631   730828   910  4157   100

Note that your objects are so small that you cannot distinguish constant cost 
(e.g. just the method dispatch on as.list) - and that is in fact what causes 
the difference - not the actual conversion:

> microbenchmark(as.list(env), as.list.environment(env),  mget(c("x", "y"), 
> env))
Unit: microseconds
 expr min  lq median  uq  max neval
 as.list(env) 4.8 5.55.9 6.1 33.1   100
 as.list.environment(env) 1.4 1.92.1 2.4  4.8   100
   mget(c("x", "y"), env) 2.6 3.13.4 3.7 37.5   100

So be careful with general statements on very small run times. Suddenly it's no 
longer surprising ...

Cheers,
S


> https://gist.github.com/hadley/5434786
> 
> Hadley
> 
> 
> 
> 
> --
> Chief Scientist, RStudio
> http://had.co.nz/
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Converting an environment to a list: mget vs. as.list

2013-04-22 Thread Hadley Wickham
Hi all,

An interesting discovery: if you want to convert an environment to a
list, and you already know the names of the objects in the
environment, using mget is about twice as fast as using as.list on the
environment.

https://gist.github.com/hadley/5434786

Hadley




--
Chief Scientist, RStudio
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel