Demo: OO Cobol
From: Robert Wagner (robert.deletethis_at_wagner.net)
Date: 06/12/04
- Previous message: Robert Wagner: "Re: OT everyone knows everyone. WAS: PPI numbers postponed until...sometime im the future!"
- Next in thread: Peter E. C. Dashwood: "Re: Demo: OO Cobol"
- Reply: Peter E. C. Dashwood: "Re: Demo: OO Cobol"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 12 Jun 2004 17:46:06 GMT
* [Continued from forest.cbl]
* This demo of object oriented Cobol continues the theme of sorts and
* trees. It inserts 50,000 random strings into a structure then does
* 50,000 lookups.
*
* One of the main attractions of OO is reusable Components. So rather
* than writing the code myself, as in previous demos, I chose to use two
* off-the-shelf Collection Classes provided by Micro Focus. One is
* called Sorted Collection and the other Dictionary. Both appear to use
* hashing to file and locate entries. Sorted Collection has a method to
* insert an entire Collection with a single operation; Dictionary does
* not. Compared to inserting entries individually, not shown here,
* inserting a Collection is substantially faster. The test data
* resides in an Ordered Collection, where the order is chronological.
* In other words, random order.
*
* Timer is now an OO Class with two methods: start and stop. Note
* how this small Class is 'nested' within a single source file.
*
* Bottom line: the Components were 42 and 59 times slower than the best
* non-OO single-threaded solution, and more then 20 times slower than
* the worst. They were 125 and 175 times slower than the multi-threading
* demo.
* ---------------------------------------------------------------------
* compile: cob -xg oodemo.cbl
$set mfoo ooctrl(+n)
$set sourceformat(free)
$SET NOBOUND OPT(2) NOTRUNC IBMCOMP
$SET NOCHECK NOPARAMCOUNTCHECK FASTCALL NOREENTRANT
$SET NOSERIAL NOFIXOPT FASTLINK
program-id. oodemo.
author. Robert Wagner.
* Object Oriented demo.
* results: Sun SPARC, 1.8 GHz, 4 CPU, time for 10 runs of 50,000 entries.
*
* Object Oriented ratio to best
* 15. Sorted Collection 106.0 59.0
* 16. Dictionary 75.0 42.0
*
* -- Results from previous demos ----
* --- ratio ---
* Sort only 2. 3. 4. 5.
* 1. Micro Focus file sort 3.7 1.0 1.5 7.4 9.3
* 2. Micro Focus table sort 3.8 1.5 7.6 9.5
* 3. qsort() 2.5 5.0 6.3
* 9-7 tree, to list 1.5 3.8
* 4. radix-insertion, to table .5 1.3
* 5. radix-insertion (list) .4 best
*
* Misc
* 6. build tree time 1.4
* 7. search all time 1.3 ratio to best
* 4+7
* Sort followed by search all or tree lookup
* 2+7 MF table sort, search 5.1 2.8
* 3+7 qsort() 3.8 2.1
* 8. tree, to table, search 3.5 1.9
* 9. tree, to table faster 2.9 1.6
* 10. tree, lookup (unbalanced) 2.6 1.4
* 11. tree, balance, lookup 2.6 1.4
* 4+7 radix-insertion, table 1.8 best
*
* Multithreaded tree build and lookup (similar to 10.) ratio to 4+7
* 12. single-threaded 1.7 .9
* 13. multithreaded with mutex 5.9 3.3
* 14. self-synchronized .6 .3
class-control.
OrderedCollection is class "ordrdcll"
SortedCollection is class "srtdclln"
Dictionary is class "dictinry"
Association is class "associtn"
CharacterArray is class "chararry"
Timer is class 'timer'
. data division
. working-storage section
* Random string (key) inserted into collections
. 01 data-area
. 05 data-key
. 10 data-key-1 pic v9(18)
. 10 data-key-2 pic v9(12)
. 01 unqualified-variables.
. 05 n comp pic s9(09)
. 05 n-limit comp pic s9(09)
. 05 c comp pic s9(09)
. 05 test-id comp pic s9(02)
. 05 repeat-factor comp pic s9(02)
. 05 a-OrderedCollection object reference.
. 05 a-SortedCollection object reference.
. 05 a-Dictionary object reference.
. 05 a-null object reference.
. 05 a-String object reference.
. 05 a-Consequent object reference.
. 05 a-Pair object reference.
. 05 a-CharacterArray object reference.
. 01 Timer-variables
. 05 a-Timer object reference
. 05 elapsed-time comp pic s9(09)
. 05 elapsed-time-edited pic zzzz.9
. procedure division.
perform construct-data
display '15. Sorted Collection' with no advancing
move 15 to test-id
perform Timer-on
perform repeat-factor times
perform construct-Collection
perform lookup-entries
perform destroy-Collection
end-perform
perform Timer-off
display '16. Dictionary' with no advancing
move 16 to test-id
perform Timer-on
perform repeat-factor times
perform construct-Collection
perform lookup-entries
perform destroy-Collection
end-perform
perform Timer-off
perform destory-data
goback
. construct-Collection.
evaluate test-id
when 15
invoke SortedCollection "ofReferences"
using n-limit
returning a-SortedCollection
invoke a-SortedCollection "addall"
using a-OrderedCollection
when 16
* A Dictionary contains Associations, an object with
* two parts: antecedent (key) and consequent (result). In this
* test, both are the same object -- a String containing a
* random number. Sub-classing Association with nulls tells
* it both parts will be objects rather than intrinsics.
invoke Association "newClass"
using a-null a-null
returning a-Pair
invoke Dictionary "ofAssociations"
using a-Pair n-limit
returning a-Dictionary
perform varying n from 1 by 1 until n > n-limit
invoke a-OrderedCollection "at"
using n
returning a-string
invoke a-Dictionary "atPut"
using a-string a-string
end-perform
end-evaluate
. lookup-entries.
perform varying n from 1 by 1 until n greater than n-limit
invoke a-OrderedCollection "at"
using n
returning a-String
evaluate test-id
when 15
invoke a-SortedCollection "occurrencesOf"
using a-String
returning c
if c not equal to 1
display 'Not found ' with no advancing
invoke a-String "display"
call 'abort'
end-if
when 16
invoke a-Dictionary "at"
using a-String
returning a-Consequent
if a-Consequent not equal to a-String
display 'Not found ' with no advancing
invoke a-String "display"
call 'abort'
end-if
end-evaluate
end-perform
. destroy-Collection.
evaluate test-id
when 15
invoke a-SortedCollection "finalize"
returning a-SortedCollection
when 16
invoke a-Dictionary "finalize"
returning a-Dictionary
end-evaluate
. construct-data.
move low-values to unqualified-variables
move 2 to repeat-factor
move 50000 to n-limit
invoke OrderedCollection "ofReferences"
using n-limit
returning a-OrderedCollection
* Construct 50,000 String objects and put references to
* them into an (un)Ordered Collection.
perform n-limit times
compute data-key-1 = function random
compute data-key-2 = function random
invoke CharacterArray "withLengthValue"
using length of data-key data-key
returning a-String
invoke a-OrderedCollection "add"
using a-String
end-perform
invoke Timer 'new' returning a-Timer
. destroy-data.
* This destroys the Collection object but not the 50,000 Strings
invoke a-OrderedCollection "finalize"
returning a-OrderedCollection
. Timer-on.
invoke a-Timer 'start'
. Timer-off.
invoke a-Timer 'stop' returning elapsed-time
compute elapsed-time-edited rounded =
(elapsed-time * (10 / repeat-factor) / 100)
display elapsed-time-edited
. end program oodemo
. Class-id. timer inherits from base
. object section
. class-control.
base is class 'base'
Timer is class 'timer'
* the following are class methods and data
. factory
. end factory
* the following are instance methods and data
. object
. working-storage section
. 01 value 0 comp-x pic x.
. 88 running value 1 false 0
. 01 start-time comp pic s9(09)
. method-id. 'start'
. local-storage section
. 01 temp-time pic x(08)
. procedure division.
if not running
accept temp-time from time
invoke self 'time-to-integer'
using temp-time returning start-time
set running to true
end-if
exit method
. end method 'start'
. method-id. 'stop'
. local-storage section
. 01 temp-time pic x(08)
. 01 end-time comp pic s9(09)
. linkage section
. 01 elapsed-time comp pic s9(09)
. procedure division returning elapsed-time.
if running
accept temp-time from time
invoke self 'time-to-integer'
using temp-time returning end-time
compute elapsed-time rounded = end-time - start-time
set running to false
end-if
exit method
. end method 'stop'
. method-id. 'time-to-integer'
. linkage section
. 01 input-time
. 05 hours pic 99
. 05 minutes pic 99
. 05 seconds pic 99
. 05 hundredths pic 99
. 01 output-time comp pic s9(9)
. procedure division using input-time returning output-time.
compute output-time =
((((((hours * 60) +
minutes) * 60) +
seconds) * 100) +
hundredths)
exit method
. end method 'time-to-integer'
. end object
. end class timer
.
- Previous message: Robert Wagner: "Re: OT everyone knows everyone. WAS: PPI numbers postponed until...sometime im the future!"
- Next in thread: Peter E. C. Dashwood: "Re: Demo: OO Cobol"
- Reply: Peter E. C. Dashwood: "Re: Demo: OO Cobol"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|