2

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;

}
3
  • 2
    It's kinda irrelevant on the blockchain. Either the transaction is executed in a single block, or it is not. A new block is added to the chain approximately every 15 seconds. It is meaningless to try and think about how long each transaction within that block "took", because the transaction is meaningless until the block is added to the chain. So you basically want to know how long it would take your transaction to be added to a block, and that depends on the gas-price that you use when you send the transaction, not on the contents (code) of the transaction. Commented Nov 10, 2019 at 6:42
  • 1
    Why don't you add your answer as a real answer @goodvibration so others see that it has been (possibly) accepted as an answer. Just one note: "either the transaction is executed in a single block or it is not" - a transaction is always executed in a single block. Commented Nov 10, 2019 at 8:46
  • 1
    @LauriPeltonen: Thanks for the advice. I meant "not executed at all". Commented Nov 10, 2019 at 10:37

2 Answers 2

1

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)

8
  • I'm not sure that it will return zero every time. Isn't it up to the system-clock of the miner's machine? In any case, it is not something to rely upon (at least not in the given resolution of now). Commented Nov 10, 2019 at 15:05
  • 1
    It will return 0 every time. Intuitive ideas of temporal time do not apply. And, although node resources are in play, real-time execution is different for every node that processes the block, so not deterministic and not computable. Transactions can be thought of as logically executing instantaneously, regardless of complexity. When blocks arrive, they contain transactions already executed, in the past. t < 0. It happened before. The individual node is merely catching up. Commented Nov 10, 2019 at 20:36
  • if the gas price is higher, the code complexity and length are much more. If it is true, the execution faster, the gas price down. From this point, I wanted to calculate the execution time for the smart contract code, from start to end of code.
    – JongH
    Commented Nov 10, 2019 at 23:26
  • 1
    Hi JohngH. May be I’m not so clear in exposition, but the fact is that it 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. Your question has one and one only answer: ZERO. In any case, worldwide, in the past, in the future. ZERO
    – Rick Park
    Commented Nov 11, 2019 at 7:37
  • Hi Rick Park, I understand some part of your explain, but I am still curious about the execution time based on solidity code which does not assume the execution time at all or just bypass. In Ethereum paradigm, how is it work for the solidity code to deploy the contract in block? It is just bypassing the solidity code before running the function in contract?
    – JongH
    Commented Nov 12, 2019 at 21:58
2

Turning a comment into an answer:

Measuring the execution-time of a transaction on the blockchain is kind of irrelevant.

Either the transaction is executed in a single block, or it is not executed at all.

A new block is added to the chain approximately every 15 seconds.

It is meaningless to try and think about how long each transaction within that block "took", because the transaction doesn't have any meaning until the block is added to the chain.

What you might want to know is - how long it would take your transaction to be added to a block.

However, the answer to this question depends on the gas-price that you use when you send the transaction, not on the contents (code) of the transaction.

3
  • As I know that the length of code and complexity are related with gas price. So, if the code reduced, the gas price should be down. Therefore, I wanted to calculate the execution speed of smart contract codes. I think that Gas price is related with length of code and complexity. I am still curious about that the execution speed is not related with gas price, but the code complexity is related with gas price.
    – JongH
    Commented Nov 10, 2019 at 23:07
  • @JongH: I'm sorry, but I cannot understand your logical reasoning in the following statement: "If the code reduced, the gas price should be down. Therefore, I wanted to calculate the execution speed of smart contract codes"... So what exactly does execution speed have to do with gas price??? You seem to have decided that there's some "mistirious" thing called "execution speed", with which you can measure your code size, and therefore the gas price. So no, there is no such thing. Commented Nov 11, 2019 at 6:11
  • 1
    And by the way, you also seem to have gotten your terminology all mixed up. It is not the gas price which is reduced when the code size is reduced, it is the gas (number of units). Gas price is something that you can set at will when you send your transaction (the higher you set it, the faster your transaction is likely to be executed (added to the blockchain). Commented Nov 11, 2019 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.