Skip to Content
高效R语言编程
book

高效R语言编程

by Colin Gillespie, Robin Lovelace
August 2018
Intermediate to advanced
227 pages
4h 16m
Chinese
China Electric Power Press Ltd.
Content preview from 高效R语言编程
56
3
4.
通过缓存变量避免不必要的计算。
5.
字节编译包可使性能轻而易举大幅提升。
一般性建议
低级程序语言如
C
语言和
Fortran
对程序员要求更多一些。它们强制你必须声
明所使用的每个变量的类型,让你负担内存管理的繁重职责,并且必须编译。
相比
R
,这类语言的长处是运行速度更快。缺点是它们需要很长时间来学习
并且不能交互式运行代码。
维基的编译器优化页面很好地介绍了标准优化技术。
R
用户无需担心数据类型。这在创建简洁代码方面是有优势的,但容易导
R
代码效率低下。虽然类似并行化之类的优化可大幅提升速度,但糟糕的
代码很容易让速度变慢,所以理解导致代码运行慢的原因很重要。
Patrick
Burns
的《
The R Inferno
》(
Lulu.com
)讲述该主题,所有上进的
R
程序员都
应该阅读一下。
最终,调用的
R
函数都是在调用底层的
C/Fortran
代码后才结束的。例如,基
本的
R
函数 runif() 仅仅包含一条代码,调用了 C_runif()
function (n, min = 0, max = 1)
.Call(C_runif, n, min, max)
R
编程的黄金法则是尽可能快地访问底层的
C/Fortran
函数,实现一个函数所
需的函数调用越少越好。例如,
x
是长度为
n
的标准向量,那么
:
x = x + 1
仅有一次
+
函数的调用,但
for
循环:
for(i in seq_len(n))
x[i] = x[i] + 1
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据科学之编程技术:使用R进行数据清理、分析与可视化

数据科学之编程技术:使用R进行数据清理、分析与可视化

迈克尔 弗里曼, 乔尔 罗斯
R数据科学

R数据科学

Hadley Wickham, Garrett Grolemund

Publisher Resources

ISBN: 9787519820855