abap--一个改变程序性能的样例,给初学者一个编程的思路,请大家指点

Suppose you have the following program. What can you do to improve performance changing the fewest lines of code?

tables: cooi,                          " Purchase Order Open Items
        ekbe.                          " PO History (paid invoices)

data: begin of po_tab occurs 10,
        refbn        like ekko-ebeln,
        mitacct(5)   type c,           " classic account
        openamt      like ppvar-wcost,
        paidamt      like ppvar-wcost,
        mitdesc(50).
data: end of po_tab.

  select * from cooi where wrttp = '22' "get po commitments
                     order by refbn.

    if w_purcord is initial.
      move cooi-refbn to w_purcord.
    endif.
    if cooi-refbn ne w_purcord.
      move cooi-refbn to w_purcord.
      perform extract_po.
    endif.
   
    move: cooi-whgbtr       to po_tab-openamt.
    if cooi-beknz = 'H'.
      po_tab-openamt = po_tab-openamt * -1.
    endif.
    collect po_tab.

    ...lots of detail processing against all po line items...

    select * from ekbe where ebeln = cooi-refbn
                         and ebelp = cooi-rfpos
                         and zekkn = cooi-rfknt+3(2)
                         and vgabe = '2'.

    ...more processing...

      move: ekbe-dmbtr         to po_tab-paidamt.
      if ekbe-shkzg = 'H'.
        po_tab-paidamt = po_tab-paidamt * -1.
      endif.
      collect po_tab.

    endselect.
  endselect.

  perform extract_po.

form extract_po.
  loop at po_tab.
    check po_tab-openamt > 0.
    ...more processing...
  endloop.
endform.
Both selects can be converted to go directly into internal tables. The following changes can cut run time down by 1/2 with very few lines of code needing to be changed.

data: t_cooi like cooi occurs 0. "add internal table like transparent one

data: begin of t_ekbe occurs 0, "add internal table w/fields of interest
        dmbtr like ekbe-dmbtr,         " amount
        shkzg like ekbe-shkzg,         " debit/credit indicator
      end of t_ekbe.

  select * from cooi into table t_cooi where wrttp = '22'.
  sort t_cooi by refbn.

  loop at t_cooi into cooi.       "places each row into table work area

    "none of the code referencing cooi needs to be changed
    "since data from the internal table is being placed into cooi

    "internal table below saves the most time because the select is
    "issued repeated times for each item within cooi (cooi holds all the
    "account assignments for all the line items for all the POs)

    select dmbtr shkzg from ekbe into table t_ekbe
                       where ebeln = cooi-refbn
                         and ebelp = cooi-rfpos
                         and zekkn = cooi-rfknt+3(2)
                         and vgabe = '2'.

    if sy-subrc <> 0.
      continue.                      "get next PO
    endif.

    loop at t_ekbe.
      move: t_ekbe-dmbtr         to po_tab-paidamt.
      if t_ekbe-shkzg = 'H'.
        po_tab-paidamt = po_tab-paidamt * -1.
      endif.
      collect po_tab.
    endloop.              "loop through po history for paid amt

  endloop.                 "loop through line items for all POs
In order to take the job from a 6 hour job to a 1 hour job, a better understanding of the underlying data was necessary. The following change was added to the above changes to complete the performance enhancement:

data: begin of t_open occurs 0, "add internal table for open docs only
        refbt like cooi-refbt,         " ref doc category (POs, reqs)
        refbn like cooi-refbn,         " ref doc number
      end of t_open.

  "only a small percentage of POs have open amounts!

  select distinct refbt refbn from cooi into table t_open
         where wrttp = '22' and
         whgbtr > 0.     "get only document numbers with open amts

  sort t_open by refbn.                "sort by document number

  loop at t_open.
  
    "process all the line items for any open PO

    select * from cooi into table t_cooi
          where refbt = t_open-refbt and  " ref doc category PO
                refbn = t_open-refbn.     "assoc w/POs w/open balances
    sort t_cooi by rfpos.

    loop at t_cooi into cooi.       

      select dmbtr shkzg from ekbe into table t_ekbe
                         where ebeln = cooi-refbn
                           and ebelp = cooi-rfpos
                           and zekkn = cooi-rfknt+3(2)
                           and vgabe = '2'.

      if sy-subrc <> 0.
        continue.                      "get next PO
      endif.

      ...lots of detail processing against OPEN po line items...

      loop at t_ekbe.
        move: t_ekbe-dmbtr         to po_tab-paidamt.
        if t_ekbe-shkzg = 'H'.
          po_tab-paidamt = po_tab-paidamt * -1.
        endif.
        collect po_tab.
      endloop.              "loop through po history for paid amt

    endloop.                "loop though all line items for each open PO

  endloop.                  "loop through open POs

  perform extract_po.

form extract_po.
  loop at po_tab.
    check po_tab-openamt > 0.   "no longer necessary because
    ...more processing...       "POs with no open amount not being
  endloop.                      "processedendform.

 

你可能感兴趣的:(编程,performance,Go)