0
0
Blockchain / Solidityprogramming~5 mins

Why Ethereum enables programmable money in Blockchain / Solidity - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why Ethereum enables programmable money
O(n)
Understanding Time Complexity

When we look at Ethereum, we want to understand how the time it takes to run programs changes as the program or data grows.

We ask: How does running smart contracts on Ethereum scale with more instructions or users?

Scenario Under Consideration

Analyze the time complexity of the following Ethereum smart contract function.


contract SimpleWallet {
    address owner;
    address[] payees;

    function addPayee(address _payee) public {
        payees.push(_payee);
    }

    function payAll() public {
        for (uint i = 0; i < payees.length; i++) {
            payable(payees[i]).transfer(1 ether);
        }
    }
}
    

This contract stores a list of payees and sends 1 ether to each payee when payAll is called.

Identify Repeating Operations

Look for loops or repeated actions in the code.

  • Primary operation: The for-loop in payAll() that sends ether to each payee.
  • How many times: It runs once for every payee in the payees list.
How Execution Grows With Input

As the number of payees grows, the number of transfers grows too.

Input Size (n)Approx. Operations
1010 transfers
100100 transfers
10001000 transfers

Pattern observation: The work grows directly with the number of payees. More payees mean more transfers.

Final Time Complexity

Time Complexity: O(n)

This means the time to run payAll() grows linearly with the number of payees.

Common Mistake

[X] Wrong: "Sending ether to all payees happens instantly no matter how many payees there are."

[OK] Correct: Each transfer takes time and gas, so more payees mean more work and longer execution.

Interview Connect

Understanding how loops affect execution time helps you explain how smart contracts behave as they handle more data, a key skill for blockchain developers.

Self-Check

What if the contract used a mapping instead of an array for payees? How would the time complexity of sending payments change?