1

Say I have a int variable called count, I was wondering how many primitive operations are int the statement:

count++;

Would it be 3? Because if you write it out in a different form, such as:

count = count + 1; 

It has 1 read, 1 operation, and 1 write. So that would mean the statement count++ counts as 3 primitive operations right?

jrbedard
  • 3,662
  • 5
  • 30
  • 34
ANewGalaxy
  • 31
  • 7

2 Answers2

3

Yes, it may be seen as having 3 operations. That means it is a constant number of operations, so for Big O notation, it is the same as if there were only 1 operation or 10, as long as it is constant.

Daniel
  • 21,933
  • 14
  • 72
  • 101
0

On most processors (or virtual machines, since you tagged your question with "java" tag) there is special instruction for incrementing value by some small amount.

i.e. see https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings - iinc

Therefore I'd count simple increment as 1 primitive operation (although internally it still gets executed as read-increment-write).

KennnyCZ
  • 11
  • 3