Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Execution time or Result time correct?

Tags:

c

A friend and me had a big discussion about a single function, the function itself does not make any sense, but it is false in my view.

The function is the following:

//get tomorrows date
int getTomorrowsDate(){
   sleep(1*60*60*24);
   return getCurrentDate();
}

If i execute the function in and we get the result, it is already wrong as the tomorrow day already got today.

After discussing this a long time, my friend is at the position that a function is correct on execution time and i am in the opposite and say a function is correct at the resulting time. May some one please elaborate to me why my view is wrong as i do not understand it.

like image 408
snapo Avatar asked Jul 16 '20 17:07

snapo


People also ask

What do you mean by execution time?

The execution time or CPU time, which we call Ci, is the total amount of time that the process executes; that time is generally independent of the initiation time but often depends on the input data. We often define deadlines for periodic processes, but we may also want to define a deadline for an aperiodic process.

What is execution time of a task?

1. The amount of time taken required by task to complete its execution.

What does execution time depend on?

The execution time of an instruction depends in part on the behavior of other instructions in the pipeline. Two instructions that compete for the same resource will execute more slowly when executed together than if they were executed at widely separated times. The memory system is an even larger source of variation.

What is the unit of execution time?

The execution time or CPU time of a given task is defined as the time spent by the system executing that task, including the time spent executing run-time or system services on its behalf. The mechanism used to measure execution time is implementation defined.


3 Answers

As David Schwartz says, status reporting operations such as getting disk free space, getting file size, checking if a file exists, etc., are fundamentally unreliable. A good way to think about them is that they return good faith estimates of their measurements, but there's the caveat that callers must not rely upon the values being correct as they could change at any time before, after, or during the measurement.

There's also the matter of requirements, both stated and unstated. Pretty much every function you write will have an unstated performance requirement that it not take 24 hours to execute. Certainly that's true for getting a date. What does it matter what you call the result of this function when the function is completely unusable? It's a purely academic distinction. In practical terms, the function is broken, and no result is ever correct.

To go further, I'm not even sure it's an academic distinction. You're asking if the "execution time" or the "result time" is correct. To be "correct" implies that the distinction will lead to either a right or wrong action and you need to know which it will be. What would those actions be?

If I'm right then I would do X, but if my friend is right I'd do Y.

What are actions X and Y? What would you do differently based on how this argument is resolved? For example, if this were a real issue in a ticketing system, would you end up closing the issue based on the outcome of your argument—neither of you fixing the 24-hour sleep? I hope not!

like image 177
John Kugelman Avatar answered Oct 11 '22 12:10

John Kugelman


A status-reporting function's result is correct if its result was valid at at least one time in-between when it was called and when it returns. This is the case for all status-reporting functions such as getting disk free space, getting file size, and so on.

At some point it has to get the status, and it can't do anything about the status changing before it gets it or after it gets it. So this has to be the rule or it would be impossible to write correct functions.

People often get this wrong. For example, they check for free space on a disk and then assume a subsequent write won't fail due to insufficient space. Or they call select and then assume a subsequent operation won't block. These are all mistakes.

like image 20
David Schwartz Avatar answered Oct 11 '22 12:10

David Schwartz


Consider this pseudo code:

fun getTomorrowsDate()
   sleep(getRandomValue())
   today = getCurrentDate() 
   sleep(getRandomValue())
   ret = today + 1
   sleep(getRandomValue())
   return ret

This is not very far from what is actually happening during EVERY function call. The operating system might interrupt at any time, so in a sense, those sleep calls actually do exist.

So unless you have taken very cautious steps of making your function atomic, the only difference between the above pseudo and your code example is that you have ensured an event that always have a non-zero probability to happen to have 100% probability.

David and John gave good answers, so I will not elaborate more. I just wanted to add this example.

like image 4
klutt Avatar answered Oct 11 '22 12:10

klutt