Solidity – How to Calculate Elapsed Time in Smart Contracts

solidity

I wanted to calculate the execution time for smart contract as below.
I tried to get the elapsed time from simple code, but the result is zero.
Does the program length size increased to calculate the elapsed time?

pragma solidity ^0.5.0;

function {

    uint startTime = now;
    uint endTime = 0;
    unit elapsedTime = 0;

    ....

    endTime = now;
    elapsedTime = endTime - startTime;

}

Best Answer

No, it is not something related to your code size!

Your code is straightforward in real-time environments. In blockchain it cannot work because your execution, by definition, starts and ends in the same block and it will return zero everytime.

It is very unlikely you need execution times in the blockchain manipulation, but something very similar to that is the gas calculation of your code. In the same way that in real-time systems you understand to having found a better way to perform a certain operation if the execution consumes less time, here you understand that if it consumes less gas.

This mirror both in a less costly execution AND in a enhanced probability to be included in the next block, that is the more similar thing to a faster execution you can achieve.

Anyway, as already said, you can pay more gas price to have similar results, but this is not the core of your question, I think.

To avoid further doubts: the fact is that this is not a situation where a certain execution time does exist, but we are not able to measure it. In Ethereum paradigm the execution time DOES NOT EXIST and for this reasons you find zero! The gas is the only way to estimate CODE COMPLEXITY as, for real time systems (NOT IN ETHEREUM) we do to optimize or sync. The question has one and one only answer: ZERO. In any case, worldwide, in the past, in the future. ZERO!

Of course any node shall consume computation power and time to evaluate the smart contract, but the time is different for different HWs and it is NOT relevant to blockchain. It is simply ignored at each node. Whatever it be at that node.

In other words: what is the temperature variation of one kilogram? And the temperature variation of one gram is higher or lower? This nonsense question is like that you are asking for: no significant answer does exist! Any (any!) realt-time-like code supposed to be able to measure that execution time, shall return ZERO.

Added after last comment:

Let’s try to focus: any ethereum node can be a different HW. From 1Ghz clock Raspeberry to a parallel multi core running at 100 Ghz. And all those node execute the same smart contract in the time frame of a block, I.e. something near 15 seconds. Any of them has a different time needed to perform the execution. What is the execution time you are arguing about? Those related to the execution of the program on the Raspberry? Let’s say it uses 280 ms. Ok. The same program uses 1.1 ms on the other cited. And all of them present the same result to solidity: I executed in the limit of one block, that is my execution time is zero for the blockchain time horizon. Your question does not have any sense in Eth environment, because Ethereum is a state machine moving from one state to the next every 15 seconds recording ALL the results achieved in the 15 seconds time frame. There is no trace of real time execution time. It does not exist. Try to focus or you will ask the same question forever. Trust us and try to understand.

(For instance start considering that “now” in solidity is not the current time, but a macro which means block.timestamp and which returns the current Ethereum block timestamp, that is the same of the previous block PLUS 15 seconds, less or more. So how can your code return anything different from zero if it is completed BEFORE the next block, I.e. it required less than 15 seconds to be completed and inserted in the current block? And if it should be not, the code simply is not executed at all by definition! So it can return zero or nothing... only)

Related Topic