abap--一个改变程序性能的样例,给初学者一个编程的思路,请大家指点
2008-01-20 09:15
453 查看
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.
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.
相关文章推荐
- abap--一个改变程序性能的样例,给初学者一个编程的思路,请大家指点
- abap--一个改变程序性能的样例,给初学者一个编程的思路,请大家指点
- abap--一个改变程序性能的样例,给初学者一个编程的思路,请大家指点
- 想做一个手机点歌的程序,希望大家进来指点
- 一个失败的程序,希望大家指点
- 作为c++初学者 又给大家带来一个小程序 英文句子的首字母大写
- 编程求21位的水仙花数,大家给讲讲思路呗,,谢谢了、、
- 求一个性能较低的算法,并可以进行并行化改造,谢谢大家!
- C/C++写得一个计时器用于检查程序的处理数据性能
- ABAP--ABAP语言对象编程的一些样例代码连接收集
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
- 微信小程序radio不能改变大小?那我自己写一个吧
- 如何调整ABAP程序的性能
- 如何调整ABAP程序的性能
- Java 程序只运行一个进程--思路
- 一些优化ABAP程序性能的方法
- SAP ABAP编程 程序中取得流水号
- ABAP--一个读取EXCEL单元格的内容超过256个字符的代码样例
- Java编程:使用Socket编写一个能多个人聊天的程序
- Windows核心编程-如何以管理员的身份启动一个程序