Constant execution performance O(1)
Constant execution time O(1)
Some algorithms will always take the same amount of time to run, regarless of the size of data you are using. The execution time remains the same. This is known as O(1).
An analogy of something that takes the same amount of time regardless of the size of the data you are using would be buying something at a supermarket till. It doesn't matter what product you are buying. The operator will pick up the item, scan it, and then put it down again. Every item takes the same amount of time to process. Each time you press a key on the keyboard, the amount of time it takes for the computer to capture the key selected is always constant. It doesn't matter what key you press. We can represent this as a graph:
We can see from this that the performance of an algorithm doesn't depend on the size of the data structure. Classic examples in programming include the push and pop operations for a stack (adding and removing data from the stack) and adding and removing data from a queue. It doesn't matter what size the stack or queue is, each of these operations will require the same number of steps to carry out.
Odd or even? An example in Python
If you give the following algorithm a number, it will calculate whether the number is odd or even. It does this by dividing the number by 2 and examining the remainder. If it is a 1, then the number must have been odd. If it is a zero, it must have been 0. It doesn't matter what size the original number is. The time that the calculation will take will always be the same.
def oddeven(num):
if num%2 == 1:
print (num, 'is odd')
else:
print (num, 'is even')
temp = int(input ('Please enter your number >>> '))
oddeven(temp)
Another example in Python
Consider the following code. It creates an array that has 10 numbers to start with between 1 and 12.
Now we will write a function called addItem to add the number 64 to our array.
When you run the algorithm to add a single piece of data to the existing list, it doesn't matter what size the array is. It will add an extra data item, regardless of whether the array is 10 elements long or 100000 elements long. It doesn't matter what size the data we want to add to the array. We can therefore describe this in Big O notation as and order of 1, or O(1).