{"id":78485,"date":"2020-05-04T16:45:07","date_gmt":"2020-05-04T11:15:07","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78485"},"modified":"2020-05-04T16:45:07","modified_gmt":"2020-05-04T11:15:07","slug":"performance-tuning-in-r","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/","title":{"rendered":"Performance Tuning in R- For Efficient R Programming"},"content":{"rendered":"<p>In this article of the <strong>TechVidvan\u2019s R tutorial series<\/strong>, we are going look at several techniques to improve R code and its performance. We shall discuss various factors that help in improving the performance of R programs and factors that should be avoided as they degrade the performance. Then we will take a look at some tips of Performance Tuning in R. We will learn how to make it faster and how to find why it is slow.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/Performance-Tuning-in-R.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78498\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/Performance-Tuning-in-R.jpg\" alt=\"Performance Tuning in R\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<h2>Performance Tuning in R<\/h2>\n<h3>1. Is R slow?<\/h3>\n<p>R was developed as a learning aid for students of statistics in 1992 by <strong>statisticians Ross Ihaka and Robert Gentleman.<\/strong> It was not developed with efficiency or speed in mind. R major advantage is its flexibility in syntax. It was created with convenience in mind. This, however, means that many important good and efficient coding practices and computer science principles are ignored in the R programming language. Essentially, R trades convenience for performance and speed. So, R is a bit slower than other modern programming languages, but that does not mean that it has to stay that way. With good syntax and coding practices, R programs can be made faster.<\/p>\n<h4>How to Write R Code That Runs Faster?<\/h4>\n<p>Here are a few tips to improve the performance of your R code:<\/p>\n<p>1. Use matrix and vector operations as much as possible. Vector and matrix operations are much less resource-intensive than operations on arrays and data frames.<\/p>\n<p>2. Use double instead of rep. To create vectors, the <strong>double()<\/strong> function is faster and less memory consuming than the <strong>rep()<\/strong> function.<\/p>\n<p>3. Avoid using data frames wherever possible. You can use matrices instead of data frames when possible as data frames can cause memory management issues and are also slower to process.<\/p>\n<p>4. Avoid creating too many objects in your environment. If there are too many objects in the environment, the execution of code and processing of data will become that much slower.<\/p>\n<p>5. Avoid changing the type and size of objects too much. Changing the type or size of an object results in the reallocation of memory which can cause memory issues and may result in the R program taking too much space.<\/p>\n<p>6. Split big objects into smaller ones to operate on them. Operating on large data objects is slow and resource-intensive. Splitting them into smaller objects and operating on them individually might be more efficient.<\/p>\n<h3>2. Functional Programming and the Memory Issues With it<\/h3>\n<p>R is primarily a functional programming language. This means that an R program can be thought of as a <a href=\"https:\/\/techvidvan.com\/tutorials\/r-functions\/\">collection of functions<\/a> and that most functions in R are <strong>first-class functions<\/strong>. This also means that objects in R are <strong>immutable<\/strong>. This means that no one can change object in R. When we change an object in any way, R creates a new object and destroys the new one. This causes memory issues.<\/p>\n<p>If a lot of altering and updating takes place, memory reallocation will take place again and again. This will result in a significant unnecessary load on the system\u2019s memory. It will also result in a load on the system processing power resulting in slower execution of R code.<\/p>\n<h3>3. Avoid Using Loops<\/h3>\n<p>R is a vector-based language. As a result, R does not need loops to operate on a set of values for example vectors. Apart from this, R also includes many functions that automatically operate iteratively on other data objects like matrices, arrays, and <a href=\"https:\/\/techvidvan.com\/tutorials\/r-data-frames\/\">data frames<\/a>. This does not mean that R programs do not require loops for anything.<\/p>\n<p>However, this also means that R is not particularly efficient in handling looping. R programmers avoid loops as much as possible and with good reason.<\/p>\n<h3>4. The Rprof Function<\/h3>\n<p>The rprof() function allows you to profile the execution of your R code. The syntax of the rprof function is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Rprof( filename = \"Rprof.out\", append = FALSE, interval = 0.02, memory.profiling = FALSE, gc.profiling = FALSE, line.profiling = FALSE, numfiles = 100L, bufsize = 10000L)<\/pre>\n<p>where,<\/p>\n<ul>\n<li><strong> filename<\/strong> is the name of the file where the result of the profiling is stored,<\/li>\n<li><strong>append<\/strong> is a logical that controls whether the results should be overwritten in the file or appended after existing contents,<\/li>\n<li><strong>interval<\/strong> specifies the real-time intervals between samples,<\/li>\n<li><strong>memory.profiling<\/strong> is a logical that controls whether the memory usage data should be stored in the output file,<\/li>\n<li><strong>gc.profiling<\/strong> is a logical that controls whether GC status should be recorded or not,<\/li>\n<li><strong>line.profiling<\/strong> is a logical that controls whether line locations should be recorded to the output file or not,<\/li>\n<li><strong>numfiles<\/strong> and <strong>bufsize<\/strong> are integers that specify the memory allocation for line profiling.<\/li>\n<\/ul>\n<p>Profiling the R code gives us the opportunity to identify any <strong>bottlenecks<\/strong> and sections of codes that may have more efficient alternatives.<\/p>\n<p>By running the <strong>rprof() function<\/strong> with a NULL argument, we can stop the profiling mode and return R to default execution.<\/p>\n<h2>Summary<\/h2>\n<p>In this chapter of the TechVidvan\u2019s R tutorial series, we learn about various techniques and tips that can help us in writing more efficient R code as well as improve the performance of our R programs. We learned about functional programming and how that affects the memory management of R programs. We also studied the drawback of using loops in R programs.<\/p>\n<p>Finally, we looked at the <a href=\"https:\/\/www.rdocumentation.org\/packages\/utils\/versions\/3.6.2\/topics\/Rprof\">Rprof() function<\/a> and how profiling helps us in identifying flaws and bottlenecks in our code. These are some of the ways of Performance Tuning in R for efficient R Programming.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article of the TechVidvan\u2019s R tutorial series, we are going look at several techniques to improve R code and its performance. We shall discuss various factors that help in improving the performance&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78498,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[2566,2567,2568],"class_list":["post-78485","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-increase-r-performance","tag-performance-tuning-in-r","tag-r-performance-tuning"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Performance Tuning in R- For Efficient R Programming - TechVidvan<\/title>\n<meta name=\"description\" content=\"Performance Tuning in R - learn easy ways how of writing more efficient R code as well as improving the performance of our R programs\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Performance Tuning in R- For Efficient R Programming - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Performance Tuning in R - learn easy ways how of writing more efficient R code as well as improving the performance of our R programs\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-04T11:15:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Performance-Tuning-in-R.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Performance Tuning in R- For Efficient R Programming - TechVidvan","description":"Performance Tuning in R - learn easy ways how of writing more efficient R code as well as improving the performance of our R programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Performance Tuning in R- For Efficient R Programming - TechVidvan","og_description":"Performance Tuning in R - learn easy ways how of writing more efficient R code as well as improving the performance of our R programs","og_url":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-05-04T11:15:07+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Performance-Tuning-in-R.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Performance Tuning in R- For Efficient R Programming","datePublished":"2020-05-04T11:15:07+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/"},"wordCount":893,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Performance-Tuning-in-R.jpg","keywords":["increase r performance","Performance Tuning in R","R performance Tuning"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/","url":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/","name":"Performance Tuning in R- For Efficient R Programming - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Performance-Tuning-in-R.jpg","datePublished":"2020-05-04T11:15:07+00:00","description":"Performance Tuning in R - learn easy ways how of writing more efficient R code as well as improving the performance of our R programs","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Performance-Tuning-in-R.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Performance-Tuning-in-R.jpg","width":802,"height":420,"caption":"Performance Tuning in R"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/performance-tuning-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Performance Tuning in R- For Efficient R Programming"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78485"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78485\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78498"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}