<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Bem lembrado Rubem! A licao nisso e':
      E' mais eficiente alocar todo o vetor em memoria antes do que
      fazer realocacoes durante o preencimento do mesmo. <br>
      <br>
      Seque abaixo um teste que fiz fazendo em paralelo com mclapply().
      O codigo original demorou 11 segundos. com mclapply() (4 cores)
      demorou 9 segundos. Ou seja, a operacao nao e' tao complicada que
      justifique a execucao em paralelo.<br>
      <br>
      <br>
      vetor <- rnorm(1499855)<br>
      ###diferenças<br>
      d<-diff(vetor, lag=1)<br>
      <br>
      k<-140<br>
      marcacoes<-NA<br>
      progress<-seq(2,(length(vetor)-k), by=1000)<br>
      tam <- (length(vetor)-k)<br>
      <br>
      t1<-Sys.time()<br>
      <br>
      system.time({<br>
        pctMaiorDepois <- rep(NA, tam)<br>
        for (i in 2:tam){<br>
          if (d[i-1] > 0) {<br>
            subVetorD<-d[(i+1):(i+k)]<br>
            pctMaiorDepois[i]<-sum(subVetorD>0)/k<br>
          }<br>
        }<br>
      })<br>
      <br>
      require(parallel)<br>
      system.time({<br>
        res <- simplify2array(mclapply(<br>
          2:tam, function(j)<br>
          if (d[j-1]>0)<br>
          return(sum(d[(j+1):(j+k)]>0)/k)<br>
          else return(NA),<br>
          mc.cores=detectCores()))<br>
      })<br>
      <br>
      all.equal(pctMaiorDepois[-1],res)<br>
      <br>
      Att.<br>
      Elias.<br>
      <br>
      On 09/02/2013 03:44 PM, Rubem Kaipper Ceratti wrote:<br>
    </div>
    <blockquote
      cite="mid:1378129466.44537.YahooMailNeo@web160702.mail.bf1.yahoo.com"
      type="cite">
      <div style="color:#000; background-color:#fff; font-family:arial,
        helvetica, sans-serif;font-size:10pt">
        <div><span>Paulo,<br>
          </span></div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><span><br>
          </span></div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><span>Creio que o problema de performance
            do seu seu código se deva ao crescimento do objeto</span> <font
            face="Courier New, courier, monaco, monospace, sans-serif">pctMaiorDepois</font><span>.
            Substitua a linha<br>
          </span></div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><span><br>
          </span></div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;">
          <font face="Courier New, courier, monaco, monospace,
            sans-serif">pctMaiorDepois<-NA
          </font><span><br>
          </span></div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><br>
        </div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;">por</div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><br>
        </div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"> <font face="Courier New, courier,
            monaco, monospace, sans-serif">pctMaiorDepois <-
            vector('numeric', tam) </font></div>
        <div><br>
        </div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><br>
        </div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><br>
        </div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;">Att.,</div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;">Rubem</div>
        <div style="color: rgb(0, 0, 0); font-size: 13.33px;
          font-family: "Arial"; background-color: transparent;
          font-style: normal;"><br>
        </div>
        <div style="font-size: 10pt; font-family: "arial",
          "helvetica", sans-serif;">
          <div style="font-size: 12pt; font-family: "times new
            roman", "new york", "times",
            serif;">
            <div dir="ltr">
              <hr size="1"> <font size="2" face="Arial"> <b><span
                    style="font-weight:bold;">De:</span></b> Paulo
                Nogueira Starzynski <a class="moz-txt-link-rfc2396E" href="mailto:paulons@gmail.com"><paulons@gmail.com></a><br>
                <b><span style="font-weight: bold;">Para:</span></b>
                R-BR <a class="moz-txt-link-rfc2396E" href="mailto:r-br@listas.c3sl.ufpr.br"><r-br@listas.c3sl.ufpr.br></a> <br>
                <b><span style="font-weight: bold;">Enviadas:</span></b>
                Domingo, 1 de Setembro de 2013 12:22<br>
                <b><span style="font-weight: bold;">Assunto:</span></b>
                [R-br] Questão de performance em processamento<br>
              </font> </div>
            <div class="y_msg_container"><br>
              <div id="yiv6309803791">
                <div dir="ltr">Senhores, <br>
                  <div>recorro a lista para buscar ajuda referente a uma
                    questão de tempo de processamento no R.</div>
                  <div>Escrevi um código para realizar, basicamente,
                    operação de contagem dentro de subvetores de um
                    vetor maior. O problema é que a performance vai
                    caindo conforme a tarefa é executada.</div>
                  <div><br>
                  </div>
                  <div>Tenho o vetor principal de dados: <b>vetor</b></div>
                  <div>Crio o vetor de diferenças vetor[i] -
                    vetor[(i-1)]: <b>d</b></div>
                  <div><br>
                  </div>
                  <div>A tarefa é simples e consiste em avaliar, a
                    partir da i-ésima posição do vetor principal, o
                    subvetor que vai de (i+1) até (i+k), calculando a
                    proporção de valores que são maiores que o valor
                    anterior dentro desse subvetor. Faço a tarefa
                    avaliando um subvetor de cada vez, reciclando o
                    mesmo objeto chamado subVetorD.</div>
                  <div>O ponto é que a cada subvetor avaliado o
                    processamento vai ficando mais lento e para vetores
                    muito longos a performance fica muito prejudicada.</div>
                  <div>A questão é: porque perde performance e o que
                    posso fazer a respeito?</div>
                  <div><br>
                  </div>
                  <div>Abaixo envio um CMR, que não o é de fato, porque
                    o vetor principal contém apenas alguns registros, a
                    caráter ilustrativo.</div>
                  <div>##########</div>
                  <div>length(vetor)<br>
                  </div>
                  <div>
                    <div>#[1] 1499855</div>
                    <div>head(vetor)<br>
                      #[1] 39.2738 39.5016 39.5299 39.4839 39.4614
                      39.6217<br>
                      summary(vetor)<br>
                      #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. <br>
                      #  14.25   31.95   50.02   46.21   57.73   89.87 </div>
                    <div><span class="yiv6309803791"
style="border-collapse:separate;color:rgb(0,0,0);font-size:14px;line-height:16px;white-space:pre-wrap;border-spacing:0px;background-color:rgb(225,226,229);">
                        <pre class="yiv6309803791" style="outline:none;border:none;margin-top:0px;margin-bottom:0px;line-height:1.2;font-size:10.4pt!important;white-space:pre-wrap!important;">
</pre>
                      </span></div>
                    <div>#diferenças</div>
                    <div>d<-diff(vetor, lag=1)</div>
                    <div><br>
                    </div>
                    <div>k<-140</div>
                    <div>pctMaiorDepois<-NA</div>
                    <div>marcacoes<-NA</div>
                    <div>progress<-seq(2,(length(vetor)-k), by=10000)</div>
                    <div>tam<-(length(vetor)-k)</div>
                    <div>t1<-Sys.time()</div>
                    <div>for (i in 2:tam){</div>
                    <div>  if (d[i-1] > 0) {</div>
                    <div>    subVetorD<-d[(i+1):(i+k)]</div>
                    <div>    pctMaiorDepois[i]<-sum(subVetorD>0)/k</div>
                    <div>  }</div>
                    <div>  if (any(i==progress)) {</div>
                    <div>    t2<-Sys.time()</div>
                    <div>    print(paste(round(i/tam,2),
                      round(difftime(t2,t1,units="secs"),2)))</div>
                    <div>    t1<-t2</div>
                    <div>  }</div>
                    <div>}</div>
                  </div>
                  <div><br>
                  </div>
                  <div>A seguir está o resultado do print() acima, que
                    parei em 70% do processamento completo, para mostrar
                    como o tempo (em segundos) vai aumentando.<br>
                  </div>
                  <div>% processamento ; tempos em segundos </div>
                  <div>
                    <div>[1] "0 0.77"</div>
                    <div>[1] "0.01 0.24"</div>
                    <div>[1] "0.01 0.33"</div>
                    <div>[1] "0.02 0.42"</div>
                    <div>[1] "0.03 0.53"</div>
                    <div>[1] "0.03 0.64"</div>
                    <div>
                      [1] "0.04 0.73"</div>
                    <div>[1] "0.05 0.86"</div>
                    <div>[1] "0.05 0.94"</div>
                    <div>[1] "0.06 1.04"</div>
                    <div>[1] "0.07 1.21"</div>
                    <div>[1] "0.07 1.27"</div>
                    <div>[1] "0.08 1.38"</div>
                    <div>[1] "0.09 1.49"</div>
                    <div>[1] "0.09 1.61"</div>
                    <div>[1] "0.1 1.79"</div>
                    <div>[1] "0.11 1.92"</div>
                    <div>[1] "0.11 2.16"</div>
                    <div>[1] "0.12 2.5"</div>
                    <div>[1] "0.13 3.1"</div>
                    <div>[1] "0.13 3.14"</div>
                    <div>[1] "0.14 3.42"</div>
                    <div>[1] "0.15 3.53"</div>
                    <div>[1] "0.15 3.74"</div>
                    <div>[1] "0.16 3.88"</div>
                    <div>[1] "0.17 3.99"</div>
                    <div>
                      [1] "0.17 4.23"</div>
                    <div>[1] "0.18 4.45"</div>
                    <div>[1] "0.19 4.66"</div>
                    <div>[1] "0.19 4.96"</div>
                    <div>[1] "0.2 5.03"</div>
                    <div>[1] "0.21 5.06"</div>
                    <div>[1] "0.21 5.63"</div>
                    <div>[1] "0.22 6.23"</div>
                    <div>[1] "0.23 6.4"</div>
                    <div>[1] "0.23 6.2"</div>
                    <div>[1] "0.24 6.71"</div>
                    <div>[1] "0.25 6.76"</div>
                    <div>[1] "0.25 6.54"</div>
                    <div>[1] "0.26 6.5"</div>
                    <div>[1] "0.27 6.72"</div>
                    <div>[1] "0.27 6.67"</div>
                    <div>[1] "0.28 6.87"</div>
                    <div>[1] "0.29 7.04"</div>
                    <div>[1] "0.29 7.51"</div>
                    <div>[1] "0.3 7.69"</div>
                    <div>[1] "0.31 7.62"</div>
                    <div>[1] "0.31 7.54"</div>
                    <div>[1] "0.32 7.82"</div>
                    <div>[1] "0.33 8.16"</div>
                    <div>[1] "0.33 8.43"</div>
                    <div>[1] "0.34 8.99"</div>
                    <div>[1] "0.35 8.55"</div>
                    <div>
                      [1] "0.35 8.98"</div>
                    <div>[1] "0.36 8.99"</div>
                    <div>[1] "0.37 9.53"</div>
                    <div>[1] "0.37 10.09"</div>
                    <div>[1] "0.38 9.88"</div>
                    <div>[1] "0.39 9.78"</div>
                    <div>[1] "0.39 10.01"</div>
                    <div>[1] "0.4 9.66"</div>
                    <div>[1] "0.41 9.8"</div>
                    <div>[1] "0.41 10.14"</div>
                    <div>[1] "0.42 10.04"</div>
                    <div>[1] "0.43 10.81"</div>
                    <div>[1] "0.43 10.94"</div>
                    <div>
                      [1] "0.44 11.05"</div>
                    <div>[1] "0.45 11.64"</div>
                    <div>[1] "0.45 11.73"</div>
                    <div>[1] "0.46 11.7"</div>
                    <div>[1] "0.47 11.65"</div>
                    <div>[1] "0.47 11.63"</div>
                    <div>
                      [1] "0.48 11.68"</div>
                    <div>[1] "0.49 12.08"</div>
                    <div>[1] "0.49 12.34"</div>
                    <div>[1] "0.5 13.35"</div>
                    <div>[1] "0.51 13.66"</div>
                    <div>[1] "0.51 13.84"</div>
                    <div>
                      [1] "0.52 13.87"</div>
                    <div>[1] "0.53 14.94"</div>
                    <div>[1] "0.53 14.42"</div>
                    <div>[1] "0.54 14.71"</div>
                    <div>[1] "0.55 14.51"</div>
                    <div>[1] "0.55 13.72"</div>
                    <div>
                      [1] "0.56 14.7"</div>
                    <div>[1] "0.57 14.75"</div>
                    <div>[1] "0.57 13.83"</div>
                    <div>[1] "0.58 14.34"</div>
                    <div>[1] "0.59 15.11"</div>
                    <div>[1] "0.59 15.09"</div>
                    <div>
                      [1] "0.6 15.12"</div>
                    <div>[1] "0.61 16.49"</div>
                    <div>[1] "0.61 18.3"</div>
                    <div>[1] "0.62 17.77"</div>
                    <div>[1] "0.63 16.87"</div>
                    <div>[1] "0.63 16.17"</div>
                    <div>[1] "0.64 17.09"</div>
                    <div>[1] "0.65 17.04"</div>
                    <div>[1] "0.65 16.38"</div>
                    <div>[1] "0.66 15.84"</div>
                    <div>[1] "0.67 16.39"</div>
                    <div>[1] "0.67 16.6"</div>
                    <div>[1] "0.68 17.32"</div>
                    <div>[1] "0.69 17.05"</div>
                    <div>[1] "0.69 17.87"</div>
                    <div>[1] "0.7 18.27"</div>
                  </div>
                  <div><br>
                  </div>
                  <div><br>
                  </div>
                  <div>Abraços,</div>
                  <div>Paulo Nogueira Starzynski</div>
                  <div><br>
                  </div>
                  <div>
                    <br>
                  </div>
                </div>
              </div>
              <br>
              _______________________________________________<br>
              R-br mailing list<br>
              <a moz-do-not-send="true"
                ymailto="mailto:R-br@listas.c3sl.ufpr.br"
                href="mailto:R-br@listas.c3sl.ufpr.br">R-br@listas.c3sl.ufpr.br</a><br>
              <a moz-do-not-send="true"
                href="https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br"
                target="_blank">https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br</a><br>
              Leia o guia de postagem (<a moz-do-not-send="true"
                href="http://www.leg.ufpr.br/r-br-guia" target="_blank">http://www.leg.ufpr.br/r-br-guia</a>)
              e forneça código mínimo reproduzível.<br>
              <br>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
R-br mailing list
<a class="moz-txt-link-abbreviated" href="mailto:R-br@listas.c3sl.ufpr.br">R-br@listas.c3sl.ufpr.br</a>
<a class="moz-txt-link-freetext" href="https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br">https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br</a>
Leia o guia de postagem (<a class="moz-txt-link-freetext" href="http://www.leg.ufpr.br/r-br-guia">http://www.leg.ufpr.br/r-br-guia</a>) e forneça código mínimo reproduzível.</pre>
    </blockquote>
    <br>
  </body>
</html>