COBOL myth busted, index versus subscript (MF on HP)




* ------ Speed of subscript v. index
------------------------------------------------------
* Findings

* Index 23
* Subscript 23
* Subscript comp-5 23
* Index 1 23
* Subscript 1 18
* Subscript 1 comp-5 18

* The last two are faster because the optimizer figured out
* it didn't need to Store the subscript on every iteration.
* It could have done that on the first four, as well.

$SET SOURCEFORMAT"FREE"
$SET NOBOUND
$SET OPT"2"
$SET NOTRUNC
$SET IBMCOMP
$SET NOCHECK
$SET ALIGN"8"
identification division.
program-id. Speed2.
author. Robert Wxagner.

data division.
working-storage section.
01 test-data.
05 comp5-number comp-5 pic s9(09) sync.

05 binary-number binary pic s9(09) sync.
05 display-number pic 9(09).
05 packed-number comp-3 pic s9(09).
05 s-subscript binary pic s9(09) sync.
05 test-byte pic x(01).

01 misaligned-area.
05 array-element occurs 4096 indexed x-index.
10 misaligned-number comp-5 pic s9(09).
10 to-cause-misalignment pic x(01).
05 byte-element occurs 4096 indexed x-index-1 pic x.

01 timer-variables.
05 test-name pic x(30).
05 repeat-factor value 100000000 binary pic s9(09).
05 current-date-structure.
10 pic x(08).
10 time-now-hhmmsshh.
15 hours pic 9(02).
15 minutes pic 9(02).
15 secs pic 9(02).
15 hundredths pic v9(02).
10 pic x(05).
05 time-now pic 9(06)v99.
05 time-start pic 9(06)v99.
05 timer-overhead value zero pic 9(06)v99.
05 elapsed-time pic s9(06)v99.
05 elapsed-time-display.
10 elapsed-time-edited pic z(05).


procedure division.

initialize test-data, misaligned-area

move 'Null test' to test-name
perform timer-on
perform timer-on
perform repeat-factor times
exit perform cycle
end-perform
perform timer-off
compute timer-overhead = (time-now - time-start)

move 'Index' to test-name
set x-index to 1000
perform timer-on
perform repeat-factor times
if x-index = 1000
set x-index up by 1
else
set x-index down by 1
end-if
move array-element (x-index) to test-byte
exit perform cycle
end-perform
perform timer-off

move 'Subscript' to test-name
move 1000 to s-subscript
perform timer-on
perform repeat-factor times
if s-subscript = 1000
add 1 to s-subscript
else
subtract 1 from s-subscript
end-if
move array-element (s-subscript) to test-byte
exit perform cycle
end-perform
perform timer-off

move 'Subscript comp-5' to test-name
move 1000 to comp5-number
perform timer-on
perform repeat-factor times
if comp5-number = 1000
add 1 to comp5-number
else
subtract 1 from comp5-number
end-if
move array-element (comp5-number) to test-byte
exit perform cycle
end-perform
perform timer-off

move 'Index 1' to test-name
set x-index-1 to 1000
perform timer-on
perform repeat-factor times
if x-index-1 = 1000
set x-index-1 up by 1
else
set x-index-1 down by 1
end-if
move byte-element (x-index-1) to test-byte
exit perform cycle
end-perform
perform timer-off

move 'Subscript 1' to test-name
move 1000 to s-subscript
perform timer-on
perform repeat-factor times
if s-subscript = 1000
add 1 to s-subscript
else
subtract 1 from s-subscript
end-if
move byte-element (s-subscript) to test-byte
exit perform cycle
end-perform
perform timer-off

move 'Subscript 1 comp-5' to test-name
move 1000 to comp5-number
perform timer-on
perform repeat-factor times
if comp5-number = 1000
add 1 to comp5-number
else
subtract 1 from comp5-number
end-if
move byte-element (comp5-number) to test-byte
exit perform cycle
end-perform
perform timer-off

goback

. timer-on.
perform read-the-time
move time-now to time-start
. timer-off.
perform read-the-time
compute elapsed-time rounded = ((time-now - time-start)
* 100000000 / repeat-factor) - timer-overhead

if elapsed-time not greater than zero
move 'error' to elapsed-time-display
else
compute elapsed-time-edited rounded = elapsed-time * 10
end-if
display test-name elapsed-time-display
. read-the-time.
accept time-now-hhmmsshh from time
*> move function current-date to current-date-structure
compute time-now =
((((hours * 60) +
minutes) * 60) +
secs) +
hundredths
.
.



Relevant Pages