Using traceTransaction we can get the following data:

debug.traceTransaction("0x0cec118d22fbd572bf25c7e4143919e608989bec7da08512f2a6f3171df3b3b8")
{
  gas: 104030,
  returnValue: "0000000000000000000000000000000000000000000000000000000000000001",
  structLogs: [{
      depth: 1,
      error: null,
      gas: 247189,
      gasCost: 3,
      memory: null,
      op: "PUSH1",
      pc: 0,
      stack: [],
      storage: {}
  }, {
      depth: 1,
      error: null,
      gas: 247186,
      gasCost: 3,
      memory: null,
      op: "PUSH1",
      pc: 2,
      stack: ["0000000000000000000000000000000000000000000000000000000000000060"],
      storage: {}
  }, 
...
  {
      depth: 1,
      error: null,
      gas: 165970,
      gasCost: 0,
      memory: ["0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000", "00000000000000000000000000000000000000000000000000000000000000c0", "532e204d61747468657700000000000000000000000000000000000000000000", "456e676c69736800000000000000000000000000000000000000000000000000", "000000000000000000000000000000000000000000000000000000000000001c", "0000000000000000000000000000000000000000000000000000000000000001"],
      op: "RETURN",
      pc: 349,
      stack: ["00000000000000000000000000000000000000000000000000000000773b82a3", "0000000000000000000000000000000000000000000000000000000000000020", "00000000000000000000000000000000000000000000000000000000000000c0"],
      storage: {
        0000000000000000000000000000000000000000000000000000000000000000: "0000000000000000000000000000000000000000000000000000000000000001",
        290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563: "532e204d61747468657700000000000000000000000000000000000000000000",
        290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e564: "456e676c69736800000000000000000000000000000000000000000000000000",
        290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e565: "000000000000000000000000000000000000000000000000000000000000001c"
      }
  }]
}

Is there some way to get the amount of time it takes for each opcode (op) to execute?

up vote 3 down vote accepted

You can get every opcode executed time by changing source file, and rebuild, deploy to test. In instructions file, there are all opcode, what you should do is add log, it's so simple. As follows:

  1. Open local instructions source file, and the adding log you want to know executing time for every opcode. For instance, i added a log for opAdd instruction:

    func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
        var startTime = time.Now().UnixNano();
        x, y := stack.pop(), stack.pop()
        stack.push(math.U256(x.Add(x, y)))
        evm.interpreter.intPool.put(y)
        fmt.Println("execute opAdd consume = ",(time.Now().UnixNano() - startTime))
        return nil, nil
    }
    
  2. Rebuild. At first go to go-ethereum dir and execute make all command in terminal. If everything is ok, some cmd had been generated in dir of 'build/bin'.

  3. Run. executing command geth to go to console, and then check result of changing code. There are six opAdd ops for traceTransaction command. execute opAdd consume = 186 execute opAdd consume = 141 execute opAdd consume = 107 execute opAdd consume = 92 execute opAdd consume = 127 execute opAdd consume = 289

Hope it helps ~

  • this approach is awesome, for sure I'm going to do this, but just now when I tried make all I got the error: core/vm/instructions.go:37: undefined: time in time.Now core/vm/instructions.go:45: undefined: time in time.Now – s.matthew.english Aug 8 '17 at 13:25
  • ok, nevermind- just had to import ("time"...) – s.matthew.english Aug 8 '17 at 13:27
  • where will these opcodes render? can I access them through the console at geth attach ipc:/Users/s.matthew.english/Library/Ethereum/testnet/geth.ipc – s.matthew.english Aug 8 '17 at 13:35
  • man, it seems I can't get this to show up in the console output thru attach – s.matthew.english Aug 8 '17 at 13:39
  • Hi, if you had added log, cd go-ethereum dir, execute ./build/bin/geth datadir=/Users/s.matthew.english/Library/Ethereum/testnet console to go to console, and then call traceTransaction method. – BinGoBinBin Aug 8 '17 at 13:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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