-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
678 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
title: "Apply vs For" | ||
author: "Miguel Carvalho Nascimento" | ||
#date: "2024-07-24" | ||
output: | ||
prettydoc::html_pretty: | ||
theme: hpstr | ||
highlight: github | ||
html_document: default | ||
pdf_document: default | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
## Comparação de Desempenho | ||
|
||
Usando o pacote microbenchmark para comparar: | ||
|
||
```{r, echo=T } | ||
#install.packages("microbenchmark") | ||
library(microbenchmark) | ||
``` | ||
|
||
#### Criando objeto para testes de desempenho: | ||
|
||
```{r, echo=T} | ||
(mat <- matrix(1:9, nrow = 3)) | ||
``` | ||
|
||
#### Exemplo com apply | ||
|
||
```{r, echo=T} | ||
(result_apply <- apply(mat, 1, sum)) | ||
``` | ||
|
||
#### Exemplo com for | ||
|
||
```{r, echo=T} | ||
result_for <- numeric(nrow(mat)) | ||
for (i in 1:nrow(mat)) { | ||
result_for[i] <- sum(mat[i, ]) | ||
};result_for | ||
``` | ||
|
||
#### Comparação | ||
|
||
```{r, echo=T} | ||
microbenchmark( | ||
apply = apply(mat, 1, sum), | ||
for_loop = { | ||
result_for <- numeric(nrow(mat)) | ||
for (i in 1:nrow(mat)) { | ||
result_for[i] <- sum(mat[i, ]) | ||
} | ||
} | ||
) | ||
``` | ||
|
||
|
||
#### Colunas Explicadas | ||
- **Unit:** microseconds: A unidade de tempo usada para medir as expressões é microsegundos (1 microsegundo = 10^-6 segundos). | ||
|
||
- **expr:** A expressão sendo avaliada. No caso, temos apply e for_loop. | ||
|
||
- **min:** O menor tempo de execução observado entre todas as execuções. apply tem um mínimo de 18.6 microsegundos, enquanto for_loop tem um mínimo de 1975.2 microsegundos. | ||
|
||
- **lq (lower quartile):** O valor do primeiro quartil, ou seja, 25% das execuções foram concluídas em menos tempo que este valor. apply tem um lq de 23.90 microsegundos, e for_loop tem um lq de 2036.85 microsegundos. | ||
|
||
- **mean:** A média dos tempos de execução. apply tem um tempo médio de 30.746 microsegundos, enquanto for_loop tem uma média de 2277.950 microsegundos. | ||
|
||
- **median:** O valor mediano dos tempos de execução, ou seja, 50% das execuções foram concluídas em menos tempo que este valor. apply tem uma mediana de 31.25 microsegundos e for_loop tem uma mediana de 2082.95 microsegundos. | ||
|
||
- **uq (upper quartile):** O valor do terceiro quartil, ou seja, 75% das execuções foram concluídas em menos tempo que este valor. apply tem um uq de 35.15 microsegundos, e for_loop tem um uq de 2220.15 microsegundos. | ||
|
||
- **max:** O maior tempo de execução observado entre todas as execuções. apply tem um máximo de 93.7 microsegundos, enquanto for_loop tem um máximo de 5675.7 microsegundos. | ||
|
||
- **neval:** O número de execuções realizadas para cada expressão. Ambas as expressões foram executadas 100 vezes. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/W32TeX) (preloaded format=pdflatex 2021.12.7) 24 JUL 2024 11:02 | ||
entering extended mode | ||
restricted \write18 enabled. | ||
%&-line parsing enabled. | ||
**Script_1.tex | ||
(./Script_1.tex | ||
LaTeX2e <2021-11-15> patch level 1 | ||
L3 programming layer <2021-11-22> (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/article.cls | ||
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/size10.clo | ||
File: size10.clo 2021/10/04 v1.4n Standard LaTeX file (size option) | ||
) | ||
\c@part=\count183 | ||
\c@section=\count184 | ||
\c@subsection=\count185 | ||
\c@subsubsection=\count186 | ||
\c@paragraph=\count187 | ||
\c@subparagraph=\count188 | ||
\c@figure=\count189 | ||
\c@table=\count190 | ||
\abovecaptionskip=\skip47 | ||
\belowcaptionskip=\skip48 | ||
\bibindent=\dimen138 | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsmath.sty | ||
Package: amsmath 2021/10/15 v2.17l AMS math features | ||
\@mathmargin=\skip49 | ||
For additional information on amsmath, use the `?' option. | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amstext.sty | ||
Package: amstext 2021/08/26 v2.01 AMS text | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsgen.sty | ||
File: amsgen.sty 1999/11/30 v2.0 generic functions | ||
\@emptytoks=\toks16 | ||
\ex@=\dimen139 | ||
)) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsbsy.sty | ||
Package: amsbsy 1999/11/29 v1.2d Bold Symbols | ||
\pmbraise@=\dimen140 | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsmath/amsopn.sty | ||
Package: amsopn 2021/08/26 v2.02 operator names | ||
) | ||
\inf@bad=\count191 | ||
LaTeX Info: Redefining \frac on input line 234. | ||
\uproot@=\count192 | ||
\leftroot@=\count193 | ||
LaTeX Info: Redefining \overline on input line 399. | ||
\classnum@=\count194 | ||
\DOTSCASE@=\count195 | ||
LaTeX Info: Redefining \ldots on input line 496. | ||
LaTeX Info: Redefining \dots on input line 499. | ||
LaTeX Info: Redefining \cdots on input line 620. | ||
\Mathstrutbox@=\box50 | ||
\strutbox@=\box51 | ||
\big@size=\dimen141 | ||
LaTeX Font Info: Redeclaring font encoding OML on input line 743. | ||
LaTeX Font Info: Redeclaring font encoding OMS on input line 744. | ||
\macc@depth=\count196 | ||
\c@MaxMatrixCols=\count197 | ||
\dotsspace@=\muskip16 | ||
\c@parentequation=\count198 | ||
\dspbrk@lvl=\count199 | ||
\tag@help=\toks17 | ||
\row@=\count266 | ||
\column@=\count267 | ||
\maxfields@=\count268 | ||
\andhelp@=\toks18 | ||
\eqnshift@=\dimen142 | ||
\alignsep@=\dimen143 | ||
\tagshift@=\dimen144 | ||
\tagwidth@=\dimen145 | ||
\totwidth@=\dimen146 | ||
\lineht@=\dimen147 | ||
\@envbody=\toks19 | ||
\multlinegap=\skip50 | ||
\multlinetaggap=\skip51 | ||
\mathdisplay@stack=\toks20 | ||
LaTeX Info: Redefining \[ on input line 2938. | ||
LaTeX Info: Redefining \] on input line 2939. | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsfonts/amssymb.sty | ||
Package: amssymb 2013/01/14 v3.01 AMS font symbols | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/amsfonts/amsfonts.sty | ||
Package: amsfonts 2013/01/14 v3.01 Basic AMSFonts support | ||
\symAMSa=\mathgroup4 | ||
\symAMSb=\mathgroup5 | ||
LaTeX Font Info: Redeclaring math symbol \hbar on input line 98. | ||
LaTeX Font Info: Overwriting math alphabet `\mathfrak' in version `bold' | ||
(Font) U/euf/m/n --> U/euf/b/n on input line 106. | ||
)) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/iftex/iftex.sty | ||
Package: iftex 2020/03/06 v1.0d TeX engine tests | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/fontenc.sty | ||
Package: fontenc 2021/04/29 v2.0v Standard LaTeX package | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/inputenc.sty | ||
Package: inputenc 2021/02/14 v1.3d Input encoding file | ||
\inpenc@prehook=\toks21 | ||
\inpenc@posthook=\toks22 | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/base/textcomp.sty | ||
Package: textcomp 2020/02/02 v2.0n Standard LaTeX package | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/lm/lmodern.sty | ||
Package: lmodern 2015/05/01 v1.6.1 Latin Modern Fonts | ||
LaTeX Font Info: Overwriting symbol font `operators' in version `normal' | ||
(Font) OT1/cmr/m/n --> OT1/lmr/m/n on input line 22. | ||
LaTeX Font Info: Overwriting symbol font `letters' in version `normal' | ||
(Font) OML/cmm/m/it --> OML/lmm/m/it on input line 23. | ||
LaTeX Font Info: Overwriting symbol font `symbols' in version `normal' | ||
(Font) OMS/cmsy/m/n --> OMS/lmsy/m/n on input line 24. | ||
LaTeX Font Info: Overwriting symbol font `largesymbols' in version `normal' | ||
(Font) OMX/cmex/m/n --> OMX/lmex/m/n on input line 25. | ||
LaTeX Font Info: Overwriting symbol font `operators' in version `bold' | ||
(Font) OT1/cmr/bx/n --> OT1/lmr/bx/n on input line 26. | ||
LaTeX Font Info: Overwriting symbol font `letters' in version `bold' | ||
(Font) OML/cmm/b/it --> OML/lmm/b/it on input line 27. | ||
LaTeX Font Info: Overwriting symbol font `symbols' in version `bold' | ||
(Font) OMS/cmsy/b/n --> OMS/lmsy/b/n on input line 28. | ||
LaTeX Font Info: Overwriting symbol font `largesymbols' in version `bold' | ||
(Font) OMX/cmex/m/n --> OMX/lmex/m/n on input line 29. | ||
LaTeX Font Info: Overwriting math alphabet `\mathbf' in version `normal' | ||
(Font) OT1/cmr/bx/n --> OT1/lmr/bx/n on input line 31. | ||
LaTeX Font Info: Overwriting math alphabet `\mathsf' in version `normal' | ||
(Font) OT1/cmss/m/n --> OT1/lmss/m/n on input line 32. | ||
LaTeX Font Info: Overwriting math alphabet `\mathit' in version `normal' | ||
(Font) OT1/cmr/m/it --> OT1/lmr/m/it on input line 33. | ||
LaTeX Font Info: Overwriting math alphabet `\mathtt' in version `normal' | ||
(Font) OT1/cmtt/m/n --> OT1/lmtt/m/n on input line 34. | ||
LaTeX Font Info: Overwriting math alphabet `\mathbf' in version `bold' | ||
(Font) OT1/cmr/bx/n --> OT1/lmr/bx/n on input line 35. | ||
LaTeX Font Info: Overwriting math alphabet `\mathsf' in version `bold' | ||
(Font) OT1/cmss/bx/n --> OT1/lmss/bx/n on input line 36. | ||
LaTeX Font Info: Overwriting math alphabet `\mathit' in version `bold' | ||
(Font) OT1/cmr/bx/it --> OT1/lmr/bx/it on input line 37. | ||
LaTeX Font Info: Overwriting math alphabet `\mathtt' in version `bold' | ||
(Font) OT1/cmtt/m/n --> OT1/lmtt/m/n on input line 38. | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/xcolor/xcolor.sty | ||
Package: xcolor 2021/10/31 v2.13 LaTeX color extensions (UK) | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics-cfg/color.cfg | ||
File: color.cfg 2016/01/02 v1.6 sample color configuration | ||
) | ||
Package xcolor Info: Driver file: pdftex.def on input line 227. | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics-def/pdftex.def | ||
File: pdftex.def 2020/10/05 v1.2a Graphics/color driver for pdftex | ||
) | ||
Package xcolor Info: Model `cmy' substituted by `cmy0' on input line 1352. | ||
Package xcolor Info: Model `hsb' substituted by `rgb' on input line 1356. | ||
Package xcolor Info: Model `RGB' extended on input line 1368. | ||
Package xcolor Info: Model `HTML' substituted by `rgb' on input line 1370. | ||
Package xcolor Info: Model `Hsb' substituted by `hsb' on input line 1371. | ||
Package xcolor Info: Model `tHsb' substituted by `hsb' on input line 1372. | ||
Package xcolor Info: Model `HSB' substituted by `hsb' on input line 1373. | ||
Package xcolor Info: Model `Gray' substituted by `gray' on input line 1374. | ||
Package xcolor Info: Model `wave' substituted by `hsb' on input line 1375. | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/geometry/geometry.sty | ||
Package: geometry 2020/01/02 v5.9 Page Geometry | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/keyval.sty | ||
Package: keyval 2014/10/28 v1.15 key=value parser (DPC) | ||
\KV@toks@=\toks23 | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/generic/iftex/ifvtex.sty | ||
Package: ifvtex 2019/10/25 v1.7 ifvtex legacy package. Use iftex instead. | ||
) | ||
\Gm@cnth=\count269 | ||
\Gm@cntv=\count270 | ||
\c@Gm@tempcnt=\count271 | ||
\Gm@bindingoffset=\dimen148 | ||
\Gm@wd@mp=\dimen149 | ||
\Gm@odd@mp=\dimen150 | ||
\Gm@even@mp=\dimen151 | ||
\Gm@layoutwidth=\dimen152 | ||
\Gm@layoutheight=\dimen153 | ||
\Gm@layouthoffset=\dimen154 | ||
\Gm@layoutvoffset=\dimen155 | ||
\Gm@dimlist=\toks24 | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/graphicx.sty | ||
Package: graphicx 2021/09/16 v1.2d Enhanced LaTeX Graphics (DPC,SPQR) | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/graphics.sty | ||
Package: graphics 2021/03/04 v1.4d Standard LaTeX Graphics (DPC,SPQR) | ||
(c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics/trig.sty | ||
Package: trig 2021/08/11 v1.11 sin cos tan (DPC) | ||
) (c:/Users/migue/AppData/Roaming/TinyTeX/texmf-dist/tex/latex/graphics-cfg/graphics.cfg | ||
File: graphics.cfg 2016/06/04 v1.11 sample graphics configuration | ||
) | ||
Package graphics Info: Driver file: pdftex.def on input line 107. | ||
) | ||
\Gin@req@height=\dimen156 | ||
\Gin@req@width=\dimen157 | ||
) | ||
|
||
! LaTeX Error: File `bookmark.sty' not found. | ||
|
||
Type X to quit or <RETURN> to proceed, | ||
or enter new name. (Default extension: sty) | ||
|
||
Enter file name: | ||
! Emergency stop. | ||
<read *> | ||
|
||
l.61 \IfFileExists | ||
{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if a... | ||
Here is how much of TeX's memory you used: | ||
3263 strings out of 480313 | ||
45258 string characters out of 5895780 | ||
336715 words of memory out of 5000000 | ||
21152 multiletter control sequences out of 15000+600000 | ||
404193 words of font info for 28 fonts, out of 8000000 for 9000 | ||
14 hyphenation exceptions out of 8191 | ||
55i,0n,62p,222b,38s stack positions out of 5000i,500n,10000p,200000b,80000s | ||
|
||
! ==> Fatal error occurred, no output PDF file produced! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
% Options for packages loaded elsewhere | ||
\PassOptionsToPackage{unicode}{hyperref} | ||
\PassOptionsToPackage{hyphens}{url} | ||
% | ||
\documentclass[ | ||
]{article} | ||
\usepackage{amsmath,amssymb} | ||
\usepackage{iftex} | ||
\ifPDFTeX | ||
\usepackage[T1]{fontenc} | ||
\usepackage[utf8]{inputenc} | ||
\usepackage{textcomp} % provide euro and other symbols | ||
\else % if luatex or xetex | ||
\usepackage{unicode-math} % this also loads fontspec | ||
\defaultfontfeatures{Scale=MatchLowercase} | ||
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1} | ||
\fi | ||
\usepackage{lmodern} | ||
\ifPDFTeX\else | ||
% xetex/luatex font selection | ||
\fi | ||
% Use upquote if available, for straight quotes in verbatim environments | ||
\IfFileExists{upquote.sty}{\usepackage{upquote}}{} | ||
\IfFileExists{microtype.sty}{% use microtype if available | ||
\usepackage[]{microtype} | ||
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts | ||
}{} | ||
\makeatletter | ||
\@ifundefined{KOMAClassName}{% if non-KOMA class | ||
\IfFileExists{parskip.sty}{% | ||
\usepackage{parskip} | ||
}{% else | ||
\setlength{\parindent}{0pt} | ||
\setlength{\parskip}{6pt plus 2pt minus 1pt}} | ||
}{% if KOMA class | ||
\KOMAoptions{parskip=half}} | ||
\makeatother | ||
\usepackage{xcolor} | ||
\usepackage[margin=1in]{geometry} | ||
\usepackage{graphicx} | ||
\makeatletter | ||
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} | ||
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi} | ||
\makeatother | ||
% Scale images if necessary, so that they will not overflow the page | ||
% margins by default, and it is still possible to overwrite the defaults | ||
% using explicit options in \includegraphics[width, height, ...]{} | ||
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} | ||
% Set default figure placement to htbp | ||
\makeatletter | ||
\def\fps@figure{htbp} | ||
\makeatother | ||
\setlength{\emergencystretch}{3em} % prevent overfull lines | ||
\providecommand{\tightlist}{% | ||
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} | ||
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering | ||
\ifLuaTeX | ||
\usepackage{selnolig} % disable illegal ligatures | ||
\fi | ||
\usepackage{bookmark} | ||
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available | ||
\urlstyle{same} | ||
\hypersetup{ | ||
pdftitle={Script\_1}, | ||
pdfauthor={Miguel Carvalho Nascimento}, | ||
hidelinks, | ||
pdfcreator={LaTeX via pandoc}} | ||
|
||
\title{Script\_1} | ||
\author{Miguel Carvalho Nascimento} | ||
\date{2024-07-24} | ||
|
||
\begin{document} | ||
\maketitle | ||
|
||
\subsection{Apply vs For}\label{apply-vs-for} | ||
|
||
\begin{itemize} | ||
\tightlist | ||
\item | ||
Comparação de Desempenho | ||
\end{itemize} | ||
|
||
Usando o pacote microbenchmark para comparar: | ||
|
||
\begin{verbatim} | ||
## Warning: package 'microbenchmark' was built under R version 4.3.3 | ||
\end{verbatim} | ||
|
||
\begin{itemize} | ||
\tightlist | ||
\item | ||
Criando objeto para testes de desempenho: | ||
\end{itemize} | ||
|
||
\subsubsection{Exemplo com apply}\label{exemplo-com-apply} | ||
|
||
\subsubsection{Exemplo com for}\label{exemplo-com-for} | ||
|
||
\subsubsection{Comparação}\label{comparauxe7uxe3o} | ||
|
||
\begin{verbatim} | ||
## Unit: microseconds | ||
## expr min lq mean median uq max neval | ||
## apply 17.5 22.45 29.344 30.05 34.40 66.2 100 | ||
## for_loop 1961.0 1984.15 2272.724 2047.10 2412.65 4536.1 100 | ||
\end{verbatim} | ||
|
||
``` | ||
\end{document} |
Oops, something went wrong.