Re: [Rd] Possible page inefficiency in do_matrix in array.c

From: Simon Urbanek <simon.urbanek_at_r-project.org>
Date: Sun, 02 Sep 2012 22:32:55 -0400

On Sep 2, 2012, at 10:04 PM, Matthew Dowle wrote:

>
> In do_matrix in src/array.c there is a type switch containing :
>
> case LGLSXP :
> for (i = 0; i < nr; i++)
> for (j = 0; j < nc; j++)
> LOGICAL(ans)[i + j * NR] = NA_LOGICAL;
>
> That seems page inefficient, iiuc. Think it should be :
>
> case LGLSXP :
> for (j = 0; j < nc; j++)
> for (i = 0; i < nr; i++)
> LOGICAL(ans)[i + j * NR] = NA_LOGICAL;
>
> or more simply :
>
> case LGLSXP :
> for (i = 0; i < nc*nr; i++)
> LOGICAL(ans)[i] = NA_LOGICAL;
>
> ( with some fine tuning required since NR is type R_xlen_t whilst i, nc
> and nr are type int ).
>
> Same goes for all the other types in that switch.
>
> This came up on Stack Overflow here :
> http://stackoverflow.com/questions/12220128/reason-for-faster-matrix-allocation-in-r
>

That is completely irrelevant - modern compilers will optimize the loops accordingly and there is no difference in speed. If you don't believe it, run benchmarks ;)

original
> microbenchmark(matrix(nrow=10000, ncol=9999), times=10)
Unit: milliseconds

                               expr      min       lq  median       uq      max
1 matrix(nrow = 10000, ncol = 9999) 940.5519 940.6644 941.136 954.7196 1409.901

swapped
> microbenchmark(matrix(nrow=10000, ncol=9999), times=10)
Unit: milliseconds

                               expr      min       lq   median      uq      max
1 matrix(nrow = 10000, ncol = 9999) 949.9638 950.6642 952.7497 961.001 1246.573

Cheers,
Simon

> Matthew
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 03 Sep 2012 - 02:35:33 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Sep 2012 - 05:50:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive