Why DeFi reimagines finance in Blockchain / Solidity - Performance Analysis
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?
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.
- Primary operation: The for-loop that goes through each user to update their balance.
- How many times: Once for every user in the list.
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 |
|---|---|
| 10 | 10 balance updates |
| 100 | 100 balance updates |
| 1000 | 1000 balance updates |
Pattern observation: The work grows directly with the number of users. Double the users, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of users.
[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.
Understanding how DeFi functions scale with users helps you explain real blockchain challenges clearly and confidently.
"What if the function used a batch update method that updated all balances at once? How would the time complexity change?"