Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/fhdsl/AI_for_software into …
Browse files Browse the repository at this point in the history
…main
  • Loading branch information
jhudsl-robot committed Jul 21, 2023
2 parents 761eee8 + e26d479 commit 0343d84
Show file tree
Hide file tree
Showing 22 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/03-writing_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ devtools::session_info()
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
6 changes: 3 additions & 3 deletions docs/04-refactoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ proc.time() - start_time

```
## user system elapsed
## 13.819 0.000 13.821
## 11.961 0.000 11.960
```

:::{.query}
Expand Down Expand Up @@ -581,7 +581,7 @@ proc.time() - start_time

```
## user system elapsed
## 0.649 0.400 0.689
## 0.573 0.339 0.604
```

The `outer()` function performs the same calculation as the nested loop in the original code, but more efficiently. It returns a matrix of all possible combinations of x and y values, with each element of the matrix being the product of the corresponding x and y values. The `rowSums()` function is then used to sum the elements of each row of the matrix, which is equivalent to summing the products of x and y for each index `i` in the original loop. This method avoids the need for the nested loop, resulting in a faster and more efficient computation.
Expand Down Expand Up @@ -609,7 +609,7 @@ proc.time() - start_time

```
## user system elapsed
## 0.317 0.304 0.393
## 0.280 0.272 0.352
```

One optimized way to perform the same calculation is by using the `%*%` operator to perform matrix multiplication. This can be done by converting x and y to matrices and transposing one of them so that their dimensions align for matrix multiplication. This code should be much faster than the original implementation because it takes advantage of highly optimized matrix multiplication algorithms in R.
Expand Down
2 changes: 1 addition & 1 deletion docs/05-annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ devtools::session_info()
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
2 changes: 1 addition & 1 deletion docs/About.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Tools Used:
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
2 changes: 1 addition & 1 deletion docs/about-the-authors.html
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ <h1>About the Authors</h1>
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
2 changes: 1 addition & 1 deletion docs/annotating-your-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ <h2><span class="header-section-number">5.10</span> Summary</h2>
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
6 changes: 1 addition & 5 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,9 @@ <h2>Lesson Breakdown</h2>
</tr>
<tr class="odd">
<td align="left"><a href="https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html#hands-on-exercise-2">Understanding Exercise</a></td>
<td align="left">10</td>
<td align="left">10 minutes</td>
</tr>
<tr class="even">
<td align="left">minutes</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><strong>Total</strong></td>
<td align="left">3 hours &amp; 10 minutes</td>
</tr>
Expand Down
3 changes: 1 addition & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ Learners will be able to:
| [Refactoring Exercise](https://hutchdatascience.org/AI_for_software/refactoring-code.html#hands-on-exercise-1) | 15 minutes |
| [Annotating Code](https://hutchdatascience.org/AI_for_software/annotating-your-code.html) | 25 minutes |
| [Understanding Code](https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html) | 25 minutes |
| [Understanding Exercise](https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html#hands-on-exercise-2) | 10
minutes |
| [Understanding Exercise](https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html#hands-on-exercise-2) | 10 minutes |
| **Total** | 3 hours & 10 minutes |


2 changes: 1 addition & 1 deletion docs/no_toc/03-writing_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ devtools::session_info()
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
6 changes: 3 additions & 3 deletions docs/no_toc/04-refactoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ proc.time() - start_time

```
## user system elapsed
## 210.341 0.064 210.403
## 231.023 0.065 231.082
```

:::{.query}
Expand Down Expand Up @@ -581,7 +581,7 @@ proc.time() - start_time

```
## user system elapsed
## 0.780 0.216 0.822
## 0.591 0.312 0.737
```

The `outer()` function performs the same calculation as the nested loop in the original code, but more efficiently. It returns a matrix of all possible combinations of x and y values, with each element of the matrix being the product of the corresponding x and y values. The `rowSums()` function is then used to sum the elements of each row of the matrix, which is equivalent to summing the products of x and y for each index `i` in the original loop. This method avoids the need for the nested loop, resulting in a faster and more efficient computation.
Expand Down Expand Up @@ -609,7 +609,7 @@ proc.time() - start_time

```
## user system elapsed
## 0.307 0.264 0.351
## 0.280 0.340 0.394
```

One optimized way to perform the same calculation is by using the `%*%` operator to perform matrix multiplication. This can be done by converting x and y to matrices and transposing one of them so that their dimensions align for matrix multiplication. This code should be much faster than the original implementation because it takes advantage of highly optimized matrix multiplication algorithms in R.
Expand Down
2 changes: 1 addition & 1 deletion docs/no_toc/05-annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ devtools::session_info()
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
2 changes: 1 addition & 1 deletion docs/no_toc/About.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Tools Used:
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
2 changes: 1 addition & 1 deletion docs/no_toc/about-the-authors.html
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ <h1>About the Authors</h1>
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
2 changes: 1 addition & 1 deletion docs/no_toc/annotating-your-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ <h2><span class="header-section-number">5.10</span> Summary</h2>
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
6 changes: 1 addition & 5 deletions docs/no_toc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,9 @@ <h2>Lesson Breakdown</h2>
</tr>
<tr class="odd">
<td align="left"><a href="https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html#hands-on-exercise-2">Understanding Exercise</a></td>
<td align="left">10</td>
<td align="left">10 minutes</td>
</tr>
<tr class="even">
<td align="left">minutes</td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><strong>Total</strong></td>
<td align="left">3 hours &amp; 10 minutes</td>
</tr>
Expand Down
3 changes: 1 addition & 2 deletions docs/no_toc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ Learners will be able to:
| [Refactoring Exercise](https://hutchdatascience.org/AI_for_software/refactoring-code.html#hands-on-exercise-1) | 15 minutes |
| [Annotating Code](https://hutchdatascience.org/AI_for_software/annotating-your-code.html) | 25 minutes |
| [Understanding Code](https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html) | 25 minutes |
| [Understanding Exercise](https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html#hands-on-exercise-2) | 10
minutes |
| [Understanding Exercise](https://hutchdatascience.org/AI_for_software/understanding-unfamiliar-code.html#hands-on-exercise-2) | 10 minutes |
| **Total** | 3 hours & 10 minutes |


6 changes: 3 additions & 3 deletions docs/no_toc/refactoring-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ <h2><span class="header-section-number">4.10</span> Code optimization</h2>
<span id="cb49-16"><a href="refactoring-code.html#cb49-16" aria-hidden="true" tabindex="-1"></a><span class="co"># End timer</span></span>
<span id="cb49-17"><a href="refactoring-code.html#cb49-17" aria-hidden="true" tabindex="-1"></a><span class="fu">proc.time</span>() <span class="sc">-</span> start_time</span></code></pre></div>
<pre><code>## user system elapsed
## 210.341 0.064 210.403</code></pre>
## 231.023 0.065 231.082</code></pre>
<div class="query">
<p>What is a faster, more optimized way of running the following R code?</p>
</div>
Expand All @@ -785,7 +785,7 @@ <h2><span class="header-section-number">4.10</span> Code optimization</h2>
<span id="cb51-12"><a href="refactoring-code.html#cb51-12" aria-hidden="true" tabindex="-1"></a><span class="co"># End timer</span></span>
<span id="cb51-13"><a href="refactoring-code.html#cb51-13" aria-hidden="true" tabindex="-1"></a><span class="fu">proc.time</span>() <span class="sc">-</span> start_time</span></code></pre></div>
<pre><code>## user system elapsed
## 0.780 0.216 0.822</code></pre>
## 0.591 0.312 0.737</code></pre>
<p>The <code>outer()</code> function performs the same calculation as the nested loop in the original code, but more efficiently. It returns a matrix of all possible combinations of x and y values, with each element of the matrix being the product of the corresponding x and y values. The <code>rowSums()</code> function is then used to sum the elements of each row of the matrix, which is equivalent to summing the products of x and y for each index <code>i</code> in the original loop. This method avoids the need for the nested loop, resulting in a faster and more efficient computation.</p>
</div>
<p>So much faster! We can look at another option by regenerating the response:</p>
Expand All @@ -804,7 +804,7 @@ <h2><span class="header-section-number">4.10</span> Code optimization</h2>
<span id="cb53-12"><a href="refactoring-code.html#cb53-12" aria-hidden="true" tabindex="-1"></a><span class="co"># End timer</span></span>
<span id="cb53-13"><a href="refactoring-code.html#cb53-13" aria-hidden="true" tabindex="-1"></a><span class="fu">proc.time</span>() <span class="sc">-</span> start_time</span></code></pre></div>
<pre><code>## user system elapsed
## 0.307 0.264 0.351</code></pre>
## 0.280 0.340 0.394</code></pre>
<p>One optimized way to perform the same calculation is by using the <code>%*%</code> operator to perform matrix multiplication. This can be done by converting x and y to matrices and transposing one of them so that their dimensions align for matrix multiplication. This code should be much faster than the original implementation because it takes advantage of highly optimized matrix multiplication algorithms in R.</p>
</div>
<p>While this second suggestion is faster, you will need to consider what aspects of the codebase are most important in each instance. For example, this code runs more quickly, but <a href="https://stat.ethz.ch/R-manual/R-patched/library/base/html/matmult.html">the <code>%*%</code> operator</a> might be unfamiliar to some R programmers. In cases where efficiency is less important, or the data are not large, you might consider maximizing readability.</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/no_toc/search_index.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/no_toc/writing-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ <h2><span class="header-section-number">3.12</span> Summary</h2>
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down
6 changes: 3 additions & 3 deletions docs/refactoring-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ <h2><span class="header-section-number">4.10</span> Code optimization</h2>
<span id="cb49-16"><a href="refactoring-code.html#cb49-16" aria-hidden="true" tabindex="-1"></a><span class="co"># End timer</span></span>
<span id="cb49-17"><a href="refactoring-code.html#cb49-17" aria-hidden="true" tabindex="-1"></a><span class="fu">proc.time</span>() <span class="sc">-</span> start_time</span></code></pre></div>
<pre><code>## user system elapsed
## 13.819 0.000 13.821</code></pre>
## 11.961 0.000 11.960</code></pre>
<div class="query">
<p>What is a faster, more optimized way of running the following R code?</p>
</div>
Expand All @@ -785,7 +785,7 @@ <h2><span class="header-section-number">4.10</span> Code optimization</h2>
<span id="cb51-12"><a href="refactoring-code.html#cb51-12" aria-hidden="true" tabindex="-1"></a><span class="co"># End timer</span></span>
<span id="cb51-13"><a href="refactoring-code.html#cb51-13" aria-hidden="true" tabindex="-1"></a><span class="fu">proc.time</span>() <span class="sc">-</span> start_time</span></code></pre></div>
<pre><code>## user system elapsed
## 0.649 0.400 0.689</code></pre>
## 0.573 0.339 0.604</code></pre>
<p>The <code>outer()</code> function performs the same calculation as the nested loop in the original code, but more efficiently. It returns a matrix of all possible combinations of x and y values, with each element of the matrix being the product of the corresponding x and y values. The <code>rowSums()</code> function is then used to sum the elements of each row of the matrix, which is equivalent to summing the products of x and y for each index <code>i</code> in the original loop. This method avoids the need for the nested loop, resulting in a faster and more efficient computation.</p>
</div>
<p>So much faster! We can look at another option by regenerating the response:</p>
Expand All @@ -804,7 +804,7 @@ <h2><span class="header-section-number">4.10</span> Code optimization</h2>
<span id="cb53-12"><a href="refactoring-code.html#cb53-12" aria-hidden="true" tabindex="-1"></a><span class="co"># End timer</span></span>
<span id="cb53-13"><a href="refactoring-code.html#cb53-13" aria-hidden="true" tabindex="-1"></a><span class="fu">proc.time</span>() <span class="sc">-</span> start_time</span></code></pre></div>
<pre><code>## user system elapsed
## 0.317 0.304 0.393</code></pre>
## 0.280 0.272 0.352</code></pre>
<p>One optimized way to perform the same calculation is by using the <code>%*%</code> operator to perform matrix multiplication. This can be done by converting x and y to matrices and transposing one of them so that their dimensions align for matrix multiplication. This code should be much faster than the original implementation because it takes advantage of highly optimized matrix multiplication algorithms in R.</p>
</div>
<p>While this second suggestion is faster, you will need to consider what aspects of the codebase are most important in each instance. For example, this code runs more quickly, but <a href="https://stat.ethz.ch/R-manual/R-patched/library/base/html/matmult.html">the <code>%*%</code> operator</a> might be unfamiliar to some R programmers. In cases where efficiency is less important, or the data are not large, you might consider maximizing readability.</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/search_index.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/writing-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ <h2><span class="header-section-number">3.12</span> Summary</h2>
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2023-07-20
## date 2023-07-21
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
Expand Down

0 comments on commit 0343d84

Please sign in to comment.