为什么矢量化速度更快 [英] Why is vectorization faster

查看:32
本文介绍了为什么矢量化速度更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 R 一段时间了,并且遇到了很多关于像我这样的编程类型以向量化操作的建议.作为一名程序员,我对为什么/如何更快感兴趣.一个例子:

I've been learning R for a while now, and have come across a lot of advice to programming types like myself to vectorize operations. Being a programmer, I'm interested as to why / how it's faster. An example:

n = 10^7
# populate with random nos
v=runif(n)
system.time({vv<-v*v; m<-mean(vv)}); m
system.time({for(i in 1:length(v)) { vv[i]<-v[i]*v[i] }; m<-mean(vv)}); m

这给了

   user  system elapsed 
   0.04    0.01    0.07 
[1] 0.3332091

   user  system elapsed 
  36.68    0.02   36.69 
[1] 0.3332091

需要考虑的最明显的事情是我们正在运行本机代码,即从 C 或 C++ 编译的机器代码,而不是解释代码,如两个示例之间用户时间的巨大差异所示(大约 3 个数量级)震级).但是还有其他事情吗?例如,R 会做什么:

The most obvious thing to consider is that we're running native code, i.e. machine code compiled from C or C++, rather than interpreted code, as shown by the massive difference in user time between the two examples (circa 3 orders of magnitude). But is there anything else going on? For example, does R do:

  • 巧妙的原生数据结构,例如存储稀疏向量或矩阵的巧妙方法,以便我们只在需要时进行乘法?

  • Cunning native data structures, e.g. clever ways of storing sparse vectors or matrices so that we only do multiplications when we need to?

惰性求值,例如在矩阵乘法中,直到需要时才计算单元格.

Lazy evaluation, e.g. on a matrix multiply, don't evaluate cells until as and when you need to.

并行处理.

别的东西.

为了测试是否可能存在一些稀疏向量优化,我尝试使用不同向量内容进行点积

To test whether there might be some sparse vector optimization I tried doing dot products with difference vector contents

# populate with random nos
v<-runif(n)
system.time({m<-v%*%v/n}); m
# populate with runs of 1 followed by 99 0s
v <-rep(rep(c(1,rep(0,99)),n/100))
system.time({m<-v%*%v/n}); m
# populate with 0s
v <-rep(0,n)
system.time({m<-v%*%v/n}); m

然而在时间上没有显着差异(大约过去了 0.09)

However there was no significant difference in time (circa 0.09 elapsed)

(Matlab 的类似问题:为什么在 MATLAB 中,矢量化代码是否比 for 循环运行得更快?)

(Similar question for Matlab: Why does vectorized code run faster than for loops in MATLAB?)

推荐答案

要考虑的最明显的事情是我们正在运行本机代码,即从 C 或 C++ 编译的机器代码,而不是解释的代码

The most obvious thing to consider is that we're running native code, i.e. machine code compiled from C or C++, rather than interpreted code

这就是大部分.另一个重要的组成部分是,由于 R 代码在其设计范式中具有功能性,因此函数(尝试)没有副作用,这意味着在某些(但可能不是全部;R 确实尝试对此保持高效)在 for 循环侧调用 [<- 的实例导致必须复制整个对象.可能会变慢.

That's most of it. The other big-ish component is that since R code is functional in its design paradigm, functions (attempt to) have no side effects, which means that in some (but perhaps not all; R does try to be efficient about this) instances calling [<- in side a for loop results in having to copy the entire object. That can get slow.

一个小的旁注:R 确实有相当广泛的功能来处理 sparse 矩阵结构有效,但它们不是默认".

A small side note: R does have rather extensive functionality for handling sparse matrix structures efficiently, but they aren't the "default".

这篇关于为什么矢量化速度更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆