Does anyone know how to create a timer in cobol?and is it possible?
4 Answers
Maybe this question of a googled page may help you: Is there a TIMER function in Cobol
On the PC search for DosSleep and on the mainframe search for ILBOWAT0.
Hope that helps.

- 47,260
- 30
- 167
- 264
To time a process in Cobol, you can calculate the difference between two points in time. Here is some sample code to measure points in time to get you started.
01 TEMP-TIME-1.
05 T1-HOURS PIC 99.
05 T1-MINUTES PIC 99.
05 T1-SECONDS PIC 99.
05 T1-HUNDREDTHS-OF-SECS PIC 99.
Use similar data definitions for TEMP-TIME-2
START-TIME
END-TIME
ACCEPT TEMP-TIME-1 FROM TIME
If using hundredth of seconds, use following loop
to start timing at start of a hundredth of a second
PERFORM WITH TEST AFTER UNTIL TEMP-TIME-2 <> TEMP-TIME-1
ACCEPT TEMP-TIME-2 FROM TIME
END-PERFORM
MOVE TEMP-TIME-2 TO START-TIME
Use similar code to set END-TIME

- 453
- 2
- 6
-
1I don't think `ACCEPT TIME` in COBOL was ever intended to build a timer function as you have illustrated. What you are doing here is burning CPU at a fantastic rate - on a mainframe system where CPU costs are real you might even be able to bankrupt your employer with this code! Final note: Testing for "not equal" in the loop could possibly lead to an infinite loop. If you are going to do this at least test for `TEMP-TIME-2 > TEMP-TIME-1` – NealB Aug 30 '12 at 14:41
-
NealB, if you want to help, please offer an alternative. If you object to the perform loop, it can be excluded, but that might reduce the accuracy of the method above. Unless time stops, temp-time-1 <> temp-temp-2 will be true within 1/100th of a second and the loop will stop. I do want to learn: please explain how an infinite loop would happen. – Valdis Grinbergs Sep 05 '12 at 02:37
-
1Efficiently implementing a timer cannot not be done using standard COBOL verbs such as `ACCEPT TIME` as you have shown. The tight loop will burn up CPU by making possibly millions of calls to timer servies - just to waste time. The correct way is to issue a call some sort of service routine to suspend your process for the desired amount of time. On a z/os based mainframe you can use either the CEE3DLY or CEEDLYM services to suspend your program - leaving the CPU free to do other things. These are non-standard routines so every OS will require a different type of call. – NealB Sep 06 '12 at 18:59
-
1How could your code result in an infinite loop? In a heavly loaded system your program is sometimes suspended by the OS for a few moments to let other processes with higher priority run. If you were unlucky, your program might be suspended for more than a 100th of a second, and that moment might have been the one you were testing for! You might think the probability of that happening is extremely small - but I wouldn't bet the farm on it not happening. – NealB Sep 06 '12 at 19:12
For OpenCOBOL there are stock library CALL functions for C$SLEEP (timed in seconds) and CBL_OC_NANOSLEEP (timed in billionths of a second - which is more resolution than most machines can handle - see librt clock_res() for access to precisions on a particular piece of hardware and OS).
Clone out a copy of OpenCOBOL sources and look in the file libcob/common.c for the cob_acuw_sleep and CBL_OC_NANOSLEEP C functions, to see how it was done for OpenCOBOL; which has conditional code for handling these timers for both Windows and for POSIXey systems.

- 3,978
- 1
- 24
- 34