0
0
Blockchain / Solidityprogramming~5 mins

Why DeFi reimagines finance in Blockchain / Solidity - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why DeFi reimagines finance
O(n)
Understanding Time Complexity

When we look at DeFi (Decentralized Finance), we want to understand how the work it does grows as more users or transactions happen.

We ask: How does the time to process transactions change when more people use DeFi?

Scenario Under Consideration

Analyze the time complexity of the following simplified DeFi smart contract function.


function distributeRewards(address[] memory users, uint256 totalReward) public {
  uint256 rewardPerUser = totalReward / users.length;
  for (uint i = 0; i < users.length; i++) {
    balances[users[i]] += rewardPerUser;
  }
}
    

This function divides a total reward equally among a list of users by looping through each user and updating their balance.

Identify Repeating Operations
  • Primary operation: The for-loop that goes through each user to update their balance.
  • How many times: Once for every user in the list.
How Execution Grows With Input

As the number of users grows, the function does more work because it updates each user's balance one by one.

Input Size (n)Approx. Operations
1010 balance updates
100100 balance updates
10001000 balance updates

Pattern observation: The work grows directly with the number of users. Double the users, double the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to finish grows in a straight line with the number of users.

Common Mistake

[X] Wrong: "The function runs in the same time no matter how many users there are."

[OK] Correct: Because the function updates each user's balance one by one, more users mean more updates and more time.

Interview Connect

Understanding how DeFi functions scale with users helps you explain real blockchain challenges clearly and confidently.

Self-Check

"What if the function used a batch update method that updated all balances at once? How would the time complexity change?"