You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 9, 2024. It is now read-only.
https://siyangming.github.io/blogs/R_28-R_program_design/
编写R代码
使用文本文件或编辑器 Always use text files / text editor
适当的缩进
限制代码的宽度
限制自定义函数的长度
缩进
缩进提高代码的可读性
限制代码的长度,避免过多的嵌套和太长的函数
最少缩进4个空格,8个更完美
程序设计
检查程序代码不同部分的执行时间对于代码优化很有用。
通常代码运行一次会表现良好,但如果你将其放如1000次循环中,运行效率是否受到影响,这时分析运行时间很有用。
优化代码
影响代码执行速度的最大因素是代码花费最多时间的部分
不通过程序执行时间分析很难完成
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil(过早最优化是万恶的根源)
–Donald Knuth
优化的一般目标
先设计后优化
过早优化是万恶的根源
操作(收集数据),不要猜测
科学家的基本素养
使用 system.time()
接收任意R表达是作为输入,返回运行该表达式所需要的时间
计算执行表达所需要的时间(秒)
返回 proc_time 类的对象
对于直接的计算任务,user time和elapsed time很接近
如果CPU花费大量的等待时间,elapsed time会比user time大很多
如果机器拥有多线程elapsted time会比user time小很多
The text was updated successfully, but these errors were encountered: