Project

General

Profile

Bug #1553

Timing SystemCommand

Added by Julian Danner over 3 years ago. Updated about 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
enhancing/improving
Target version:
Start date:
17 Dec 2020
Due date:
% Done:

100%

Estimated time:
Spent time:

Description

Hi,

I was just timing some computations and noticed that the the time it takes to execute a command with SystemCommand is not accounted for with CpuTime.
While the manual more or less says that this is the expected behaviour, this is also quite inconvenient, since it is now not possible to accurately time any computations that involve calls to SystemCommand. :/

Example:

t0:=CpuTime();
SystemCommand("sleep 10");
TimeFrom(t0);

Yes, I could time these computations using TimeOfDay(), but that only has a precision of seconds and may only bring up problems when timing around midnight...

(Here is also a good spot to mention that I miss the simple but often useful time command that (AFAIR) CoCoA-4 offered ;) )

2021-03 Added function for computing the total time: ElapsedTime see #1553-7


Related issues

Related to CoCoA-5 - Feature #1583: New fn ElapsedTimeFrom?New2021-03-19

History

#1 Updated by John Abbott over 3 years ago

  • Category set to enhancing/improving
  • Target version set to CoCoA-5.4.2

First comment: using sleep is perhaps not the best test. Below is what I get using bash on my Linux computer:

$ time sleep 10

real    0m10.009s
user    0m0.000s
sys    0m0.006s

The CpuTime function measures "cpu-time" (which is the same as "user time", and may be considerably less than "wall-clock-time").

Another problem is that the command executed by SystemCommand is a separate process, so it is not entirely clear that its time should be counted time consumed by CoCoA.
It is also not so easy to find a portable solution... Microsoft likes to do things its own way... :-/ And I am little inclined to waste my time working around Microsoft's deliberate obstructionism.

#2 Updated by John Abbott over 3 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 10

I have searched briefly (or not-so-briefly) on internet, and it seems that C++ does not yet offer a way to measure process "user/system time".
As I mentioned above this would in any case probably not cover the resource consumption of a separate process (is it a child process?)

There is a way to measure differences of "wall clock time" using std::chrono::steady_clock. Exporting this to CoCoA-5 is probably not altogether immediate/trivial

Do we want a reliable means of measuring "real time" intervals in CoCoA-5?
(since it is a standard C++ function it should even work on Microsoft)

#3 Updated by Julian Danner over 3 years ago

oh well yes, sleep may not be the best test, however I think it nonetheless highlights my problem ;)

I agree that it is not entirely clear that the process created by SystemCommand should be accounted for by CpuTime and by that counted as time consumed by CoCoA (at least the manual of it is a bit unclear in that regards), but still it would be nice to have some function to measure the spent time of SystemCommand. (Otherwise I'll have to write a workaround that times this externally and then again read back into CoCoA.)

Is it possible to change the implementation of SystemCommand such that the command is launched as a child process whose time is then considered by CoCoA?

Do we want a reliable means of measuring "real time" intervals in CoCoA-5?
(since it is a standard C++ function it should even work on Microsoft)

Yes that would also be sufficient for my needs ;)

#4 Updated by John Abbott over 3 years ago

It might be possible to replace the call to the C++ function system, by a fork and an exec (or execv?).
I have little experience with such "low-level" commands, and would be quite happy to delegate investigating the matter to someone else!

The "real time elapsed" might be relatively easy (since it uses standard C++).

#5 Updated by John Abbott over 3 years ago

I have now implemented ElapsedTime (based on the standard C++ steady_clock).

/**/ FloatStr(ElapsedTime()); SleepFor(5); FloatStr(ElapsedTime());
51.345
56.346

Like CpuTime() the internal result is a double which is then converted exactly into a rational. This means that one must use FloatStr to print out the result in a comprehensible way. I wonder whether it may not simply be easier to make it return, say, milliseconds (as an integer)?

#6 Updated by Julian Danner over 3 years ago

The new func ElapsedTime is perfectly fine with me!
I personally prefer the return value representing a rational as secs over an integer as millisecs, among others to also have it consistent with CpuTime.

#7 Updated by John Abbott over 3 years ago

  • Status changed from In Progress to Feedback
  • Assignee set to John Abbott
  • % Done changed from 10 to 70

I have checked in ElapsedTime (incl. doc).

New qn: is the fn RealTime in CoCoALib obsolete? -- NOTE: maybe not obsolete because it can be used when seeding RNG.

#8 Updated by Anna Maria Bigatti over 3 years ago

  • Target version changed from CoCoA-5.4.2 to CoCoA-5.4.0

#9 Updated by John Abbott about 3 years ago

  • % Done changed from 70 to 90

#10 Updated by Anna Maria Bigatti about 3 years ago

should we also add the Elapsed counterpart of TimeFrom?
(new issue #1583)

#11 Updated by Anna Maria Bigatti about 3 years ago

  • Description updated (diff)

#12 Updated by Anna Maria Bigatti about 3 years ago

  • Description updated (diff)
  • Status changed from Feedback to Closed
  • % Done changed from 90 to 100

#13 Updated by Anna Maria Bigatti about 3 years ago

  • Description updated (diff)

#14 Updated by Anna Maria Bigatti about 3 years ago

  • Description updated (diff)

#15 Updated by Anna Maria Bigatti about 3 years ago

Also available in: Atom PDF