zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    问题记录之spring-mvc.xml配置文件报错
    问题记录之用poi生成图片并插入到word时,图片中的中文显示不出来
    问题记录之每次打开vmware的时候都会跳出xftp的安装程序问题解决
    DataTable和List之间互转
    .NET[C#]中实现实体对象深拷贝(克隆/复制)的几种方法
    SQL Server表分区详解(转)
    vs2017使用GitHub插件发布项目到github
    jquery autocomplete 在IE11中出现打开网页输入框有默认值时下拉列表是已经打开的状态解决
    MVC DropDownLis 二级联动实现
    sql 查询某个表在哪些存储过程(SP)中使用到
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157103.html
Copyright © 2011-2022 走看看