Switch vs if instruction or maybe there is better way...

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!

My question concern switch instruction.

I have like 14 instructions which need to be done according to i number.The initial number is 4.
Then according to switch press I can increase i or decrease it.
I can use switch instruction or if to check what is i but I feel it is not perfect. If I increase or decrease I know number can be 3 or 5 so why check all numbers from 1 to 14?

I don`t know If I correctly explained my question but I hope you understood me...

Below I typed code which is clear but I think it is not the best solution.

Further explanation:
I am searching something like if i is equal to 4 then do instructions from table[i] if i is inreased then do something from table[i+1]- I don`t need to check what is i each time...

if (get_key_press(1<<0))// Do gory
	{
		i++;
		
		switch (i)
		{
			case 0:
				
						
			break;
			
			case 1:
			
			break;
			
			case 2:
			
			break;
			
			case 3:
			
			break;
			
			case 4:
			
			break;
			
			case 5:
			
			break;
			
			case 6:
			
			break;
			
			case 7:
			
			break;
			
		}
		
		
		
				
	}	
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It doesn't really matter does it? I guess if you had hundreds of cases and the compiler chose to generate an if/else cascade for the switch() it could take a while if the value was high. But on the whole if you use switch() and the cases are incremental the compiler will likely generate a lookup table where it does a computed goto based on the switch variable so it will just takle i, use it to index an array of destination addresses then IJMP to that table entry - as such it doesn't matter how many cases there are.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you use switch() the compiler will choose the best code for you.

If you choose an if-else cascade, the compiler will have to use your method, regardless.

If you want some cases to be tested first, if-else will ensure the order. If your switch() block has some fall-throughs the compiler will be obliged to follow your order even if it has better methods.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
If your switch() block has some fall-throughs the compiler will be obliged to follow your order even if it has better methods.

Not necessarily, it may still generate an optimized jump table, with jumps at the end of the fall-through blocks to their associated follow-up block.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since the input is ultimately depending on a human I believe it follows that execution speed need not be hard-optimized. If code size is not critical, the optimization that should take precedence is optimization for maintenance of the code.

Use the code-constructs that most clearly conveys "what is happening", and that you think will be most receptive for anticipated future changes.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some compilers like the ImageCraft one insist for some reason on generating 16 bit compares for each case, even if the switch argument is just a char.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jayjay1974 wrote:
Some compilers like the ImageCraft one insist for some reason on generating 16 bit compares for each case, even if the switch argument is just a char.

that would be a standards compliant evaluation.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
compiler will likely generate a lookup table where it does a computed goto based on the switch variable so it will just takle i, use it to index an array of destination addresses then IJMP to that table entry - as such it doesn't matter how many cases there are.

This explained my question. Thank you guys...