0
0
Blockchain / Solidityprogramming~5 mins

Why logic controls execution in Blockchain / Solidity - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why logic controls execution
O(n)
Understanding Time Complexity

In blockchain programming, the logic you write decides how many steps your code takes.

We want to see how this logic affects the time it takes to run as input grows.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


function processTransactions(transactions) {
  for (let i = 0; i < transactions.length; i++) {
    if (transactions[i].amount > 1000) {
      validateTransaction(transactions[i]);
    }
  }
}

function validateTransaction(tx) {
  // some validation logic here
}
    

This code checks each transaction and only validates those over 1000 units.

Identify Repeating Operations
  • Primary operation: Looping through all transactions once.
  • How many times: Once for each transaction in the list.
How Execution Grows With Input

As the number of transactions grows, the code checks each one once.

Input Size (n)Approx. Operations
10About 10 checks
100About 100 checks
1000About 1000 checks

Pattern observation: The work grows directly with the number of transactions.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows in a straight line as the input size grows.

Common Mistake

[X] Wrong: "Since only some transactions are validated, the code runs faster than checking all."

[OK] Correct: The loop still checks every transaction, so the time grows with all transactions, not just the validated ones.

Interview Connect

Understanding how your logic controls execution helps you write efficient blockchain code and explain it clearly in conversations.

Self-Check

"What if we added a nested loop to validate transactions against a list of rules? How would the time complexity change?"