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
Copy file name to clipboardExpand all lines: docs/04-refactoring.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -559,7 +559,7 @@ proc.time() - start_time
559
559
560
560
```
561
561
## user system elapsed
562
-
## 8.472 0.000 8.472
562
+
## 8.189 0.000 8.188
563
563
```
564
564
565
565
:::{.query}
@@ -587,7 +587,7 @@ proc.time() - start_time
587
587
588
588
```
589
589
## user system elapsed
590
-
## 0.744 0.600 0.646
590
+
## 0.765 0.571 0.650
591
591
```
592
592
593
593
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.
@@ -615,7 +615,7 @@ proc.time() - start_time
615
615
616
616
```
617
617
## user system elapsed
618
-
## 0.364 0.259 0.262
618
+
## 0.319 0.317 0.273
619
619
```
620
620
621
621
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.
0 commit comments