<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: High CPU Usage in a Loop / Read statement in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411672#M1242642</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;sort all the tables that you are using.&lt;/P&gt;&lt;P&gt;Use binary search in all read statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;IF itab-matnr NE tmpmatnr.&lt;/P&gt;&lt;P&gt;      mat_new = 'T'.&lt;/P&gt;&lt;P&gt;      tmpmatnr = itab-matnr.&lt;/P&gt;&lt;P&gt;rather than doing this &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;delete adjacent duplicates from itab comparing matnr.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Lalit Mohan Gupta.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Apr 2009 05:14:47 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2009-04-12T05:14:47Z</dc:date>
    <item>
      <title>High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411666#M1242636</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a itab with around 190k lines of data. When I loop it and do a few READ on other itabs, the CPU resources almost hit 100%. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; At times I've more complicated series of looping and reading but yet the CPU wouldn't even go near to 100%. Any idea why it causes the CPU to hit nearly 100% usage in this case?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
  LOOP AT itab.

    IF itab-matnr NE tmpmatnr.
      mat_new = 'T'.
      tmpmatnr = itab-matnr.

* Stock in MS10 + CS10 Location
      READ TABLE stock WITH KEY matnr = itab-matnr
                                locn = 'MS'.
      IF sy-subrc = 0.
        it_tab-qohqty = stock-quant.
      ELSE.
        it_tab-qohqty = 0.
      ENDIF.

* Stock in VM10 Locn
      READ TABLE stock WITH KEY matnr = itab-matnr
                                locn = 'VM'.
      IF sy-subrc = 0.
        it_tab-vmiqty = stock-quant.
      ELSE.
        it_tab-vmiqty = 0.
      ENDIF.

      IF vm_excld = ''.
        ADD  it_tab-vmiqty TO  it_tab-qohqty.
      ENDIF.

* IQC Stock in RS10 location
      READ TABLE stock WITH KEY matnr = itab-matnr
                                locn = 'RS'.
      IF sy-subrc = 0.
        it_tab-iqcqty = stock-quant.
      ELSE.
        it_tab-iqcqty = 0.
      ENDIF.

* WIP Stock
      it_tab-wipqty = 0.

      IF rdb1 = 'X'.
        READ TABLE stock WITH KEY matnr = itab-matnr
                                  locn = 'WP'.
        IF sy-subrc = 0.
          it_tab-wipqty = stock-quant.
        ENDIF.
      ELSE.
        READ TABLE subconstk WITH KEY matnr = itab-matnr.
        IF sy-subrc = 0.
          it_tab-wipqty = subconstk-lblab.
        ENDIF.
      ENDIF.

      IF rdb6 = 'X'.                "Plant level Stock
        it_tab-wipqty = it_tab-wipqty + it_tab-qohqty.
      ENDIF.
    ELSE.
* Current WIP Qty = Prev Shortage Qty.
      it_tab-wipqty = it_tab-shtqty.
    ENDIF.

* Current Shortage qty = Available WIP Qty - Current requirements
    it_tab-shtqty = it_tab-wipqty - itab-reqqty.

    IF rdb1 = 'X' AND itab-disp_flag &amp;lt;&amp;gt; 'T'.
      CONTINUE.
    ENDIF.

    IF rdb3 = 'X'.
      CHECK it_tab-shtqty &amp;lt; 0.
    ENDIF.

    READ TABLE tmakt WITH KEY matnr = itab-matnr.
    it_tab-matnr = itab-matnr.
    it_tab-maktx = tmakt-maktx.
    it_tab-aufnr = itab-aufnr.
    it_tab-qty_per = itab-qty_per.
    it_tab-plnbez = itab-plnbez.
    it_tab-gstrp = itab-gstrp.
    it_tab-prdqty = itab-bomqty.
    it_tab-reqqty = itab-reqqty.

    APPEND it_tab.

    parts-matnr = it_tab-matnr.
    APPEND parts.
  ENDLOOP.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Mar 2009 02:33:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411666#M1242636</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-27T02:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411667#M1242637</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sort table stock by MATNR LOCN and use binary search with read statement.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SORT STOCK BY MATNR LOCN.
....
READ TABLE STOCK WITH KEY MATNR = ITAB-MATNR
                          LOCN = 'MS' 
                          BINARY SEARCH.
* and so on ...
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Mar 2009 05:42:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411667#M1242637</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-27T05:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411668#M1242638</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Inorder to improve the performance during READ statements, sort the table which u want to read and then use the READ statement with the BINARY SEARCH addition.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kindly note that the internal table needs to be SORTed and READ with the same fields.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Susanth Kumar&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Mar 2009 06:29:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411668#M1242638</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-27T06:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411669#M1242639</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;  1. Sort the table Stock table with Matnr and location wise. &lt;/P&gt;&lt;P&gt;  2. Also sort the Internal table Itab&lt;/P&gt;&lt;P&gt;  3. Use Work Area Like &lt;/P&gt;&lt;P&gt;	LOOP AT itab into wa.&lt;/P&gt;&lt;P&gt;  4. Rewrite your Read statement like &lt;/P&gt;&lt;P&gt;	&lt;/P&gt;&lt;P&gt;    READ TABLE stock INTO Wa_stock WITH KEY matnr  = WA-Matnr &lt;/P&gt;&lt;P&gt;                                                   			       Locn  =  u2018u2019MS&lt;/P&gt;&lt;P&gt;                                                                                BINARY SEARCH.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;  5. Donu2019t use Internal table header line.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Mar 2009 06:49:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411669#M1242639</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-27T06:49:43Z</dc:date>
    </item>
    <item>
      <title>Re: High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411670#M1242640</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't really undestand what you mean with CPU 100% .... is your SAP system on your PC?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;About internal tables, please read my blog&lt;/P&gt;&lt;P&gt;Measurements on internal tables: Reads and Loops:&lt;/P&gt;&lt;P&gt;/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Check what SAP basis release you use, in releases from 6.10 onward I would &lt;/P&gt;&lt;P&gt;recommend to check sorted or hashed tables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Otherwise SORT  table stock before the loop and use READ BINARY SEARCH, both with the key fields.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;same with the other table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For the LOOP use ASSIGNING into a field-symbol, but use typing for the field-symbol.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will tremedously speed up your application.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Siegfried&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Mar 2009 08:44:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411670#M1242640</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-03-27T08:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411671#M1242641</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;First Sort the table and use Binary search in READ statement.&lt;/P&gt;&lt;P&gt;It will increase the performance&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Mar 2009 07:04:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411671#M1242641</guid>
      <dc:creator>former_member506713</dc:creator>
      <dc:date>2009-03-31T07:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411672#M1242642</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;sort all the tables that you are using.&lt;/P&gt;&lt;P&gt;Use binary search in all read statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;IF itab-matnr NE tmpmatnr.&lt;/P&gt;&lt;P&gt;      mat_new = 'T'.&lt;/P&gt;&lt;P&gt;      tmpmatnr = itab-matnr.&lt;/P&gt;&lt;P&gt;rather than doing this &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;delete adjacent duplicates from itab comparing matnr.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Lalit Mohan Gupta.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Apr 2009 05:14:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411672#M1242642</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-12T05:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: High CPU Usage in a Loop / Read statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411673#M1242643</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Here are few tips:&lt;/P&gt;&lt;P&gt;1. Use internal table without header lines and use explicit work area. Better is to use field symbols while looping. Field symbols give better performance as it is directly accessing the record in an internal table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. For read statements use binary search. If possible use hashed table.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Apr 2009 13:31:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/high-cpu-usage-in-a-loop-read-statement/m-p/5411673#M1242643</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-13T13:31:52Z</dc:date>
    </item>
  </channel>
</rss>

