0
0
Blockchain / Solidityprogramming~5 mins

Sending Ether (transfer, send, call) in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Sending Ether (transfer, send, call)
O(n)
Understanding Time Complexity

When sending Ether in blockchain smart contracts, it's important to understand how the time cost grows as the number of transactions or calls increases.

We want to know how the execution time changes when sending Ether using different methods like transfer, send, or call.

Scenario Under Consideration

Analyze the time complexity of the following Solidity code snippet that sends Ether to multiple addresses.


function sendEther(address[] memory recipients) public payable {
    for (uint i = 0; i < recipients.length; i++) {
        payable(recipients[i]).transfer(1 ether);
    }
}
    

This code sends 1 Ether to each address in the recipients list using the transfer method inside a loop.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The for-loop that sends Ether to each recipient.
  • How many times: The loop runs once for every address in the recipients array.
How Execution Grows With Input

Each additional recipient means one more transfer operation, so the total work grows directly with the number of recipients.

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

Pattern observation: The number of operations grows in a straight line as the input size increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to send Ether grows directly in proportion to the number of recipients.

Common Mistake

[X] Wrong: "Sending Ether to multiple addresses happens instantly no matter how many recipients there are."

[OK] Correct: Each transfer requires a separate operation, so more recipients mean more work and longer execution time.

Interview Connect

Understanding how sending Ether scales helps you explain gas costs and transaction limits clearly, showing you grasp practical blockchain programming challenges.

Self-Check

"What if we replaced transfer with call inside the loop? How would the time complexity change?"