Efficiency of matrix rowSums() vs. colSums() in R vs Rcpp vs Armadillo

前端 未结 2 1514
北恋
北恋 2021-02-19 19:25

Background

Coming from R programming, I\'m in the process of expanding to compiled code in the form of C/C++ with Rcpp. As a hands on exercise on the

2条回答
  •  太阳男子
    2021-02-19 20:01

    "why is Cpp_rowSums() significantly faster than Cpp_colSums()?" - when fetching "row major" the CPUs prefetcher can predict what you are doing and fetch the next bunch of data you need from main memory to the CPUs cache before you need it. This speeds up your access to the data.

    When you access "column major" the prefetcher has a much harder job predicting what you are going to need next, so it won't be stuffing things into cache memory ahead of time as efficiently (if at all) - this slows you down.

    CPUs love linear access to data. If you don't do what they love you pay the price of cache misses and main memory access latencies.

提交回复
热议问题