Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3760304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:27:04+00:00 2026-05-19T10:27:04+00:00

I am a newbie to R. Assume the memory layout is the same for

  • 0

I am a newbie to R. Assume the memory layout is the same for data frame and matrix.

In the following matrix

a=matrix(1:10000000,1000000,10)

it has 1M rows and 10 columns. Is the memory for row or for column sequential physically? Or is the physical memory first store [1,1],[2,1],[3,1],,[1M,1],[2,1] or [1,2],[1,2],..[1,10],[2,1]…?

Suppose the matrix with 10M element is of size 100M, and the L2 cache is 4M, then L2 cache can’t store all these 10M element. If we process the data sequentially, we will have less L2 cache missing ratio. For our case, we need to process row by row and read several columns at the same time, such as column A, B, C, and then create some result. If the layout of the memory is first store 10 items in 1st row, then store 10 items in the 2nd row, then the performance might be better.

If there any way to control the memory layout?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:27:04+00:00Added an answer on May 19, 2026 at 10:27 am

    A matrix is simply a vector with a dim attribute. The elements of the matrix are stored in the vector in column-major order. There is no way to change this.

    Therefore, if you need to operate row-by-row, it’s faster to transpose the matrix before looping over it.

    > set.seed(21)
    > a = matrix(rnorm(1e6),1e3,1e3)
    > ta = t(a)
    > system.time(for(i in 1:1000) colSums(ta))
       user  system elapsed 
       1.39    0.00    1.40 
    > system.time(for(i in 1:1000) rowSums(a))
       user  system elapsed 
       2.40    0.00    2.39 
    > identical(rowSums(a), colSums(ta))
    [1] TRUE
    

    If you want to dig deeper, the code for colSums, rowSums, colMeans, and rowMeans is in the do_colsum function in src/main/array.c.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let assume one table is having 3 row id | data ---------- 1 |
A newbie question. I have the following piece of Java code: import acm.program.*; import
OK - Python newbie here - I assume I am doing something really stupid,
I am a newbie of using c++. I am following a tutorial to get
Say I am using the following interface (assume we can't change our implementation to
Newbie question... The objective: I intend to have an HTML text input field as
Newbie question: I just installed VisualSVN Server and created a repository. I noticed that
Newbie question here. I'm in the beginning stages of laying out a site in
Newbie question: There are three types of Asp.Net controls : HTML server controls, Web
newbie for clearcase. Since clearcase's config is rather different from other concept in git,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.