I'm using online solidity compiler and would like to measure the CPU time for executing a function by capturing the time at start and end of the function.

I found example for Javascript, but when tried it the compiler does not recognise 'var'

var start = new Date().getTime();
var amount = 1;
var sender = personal.listAccounts[0]
var receiver = personal.listAccounts[0]
for (i = 0; i < 5000; ++i) {
   eth.sendTransaction({from:sender, to:receiver, value: amount});
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ' + time);

How to implement this here?


4 Answers 4


you can't recognise var cause solidity don't use it. Solidity have other types for store the variables, and this is a solidity compiler not a javascript compiler.

For learn abaout types in Solidity go here.

And for structures here.

  • thanks! but any way to get around this?
    – MWH
    Jul 27, 2017 at 13:58
  • i'm thinking you are using the online solidity compiler for "test" javascript and this compiler is not for this, so what you need? a online compiler? or something with ethereum or solidity?
    – Gawey
    Jul 27, 2017 at 14:01
  • Thanks do you have any idea how to get this even on Ethereum private network for instance where i send a transaction and then get its execution time!
    – MWH
    Jul 27, 2017 at 14:04
  • Maybe if you use a confirmation of the transaction by callback function and using the web3.js you can have a some approx time but not the exact time cause it depends. But i thinks this will work :/
    – Gawey
    Jul 27, 2017 at 14:07

That code is not Solidity and thus can not be compiled with the Solidity compiler.

If you compile a contract and submit it to the blockchain you can then call a method defined within.

You can not calculate how long it takes for a function to execute. At least not accurately. Function execution depends on the execution transaction being mined. The transaction will be mined quicker if you set a higher gas price for example.

You could approximate the timings by discerning the time that the transaction was submitted and the time that the block containing the transaction was mined, but I am unsure as to how this would be useful.

  • Thank you! i am not concerning about how long a transaction takes to be added to the blockchain, but rather to measure the actual execution time for the transaction (not even concern about time takes to mine a block). Hope made it clearer!
    – MWH
    Jul 27, 2017 at 14:02
  • They are the same. Jul 27, 2017 at 14:04
  • How? even if i run private network but measure time of executing a transaction without considering gas price
    – MWH
    Jul 27, 2017 at 14:08
  • Then the time for the transaction to execute is the time it takes you to mine the block on your private network. Jul 27, 2017 at 14:09
  • No what i want is to only transaction time without taking into account the block time; just run a transaction and measure its time, would that be possible?
    – MWH
    Jul 27, 2017 at 14:20

In Ethereum, measure of execution complexity is gas consumption rather than CPU. When you execute any function of your smart contract in Remix, console shows you short execution summary. At the very right of this summary there is angle arrow pointing down. Press it and you will see execution stats including "execution cost". This is probably what you need.


Apart comments on your code, that time you are trying to measure is always zero, I.e. it is not measurable in terms of CPU effort

See: Solidity: Is there any way to calculate the elapsed time for smart contract?

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