Re: [algogeeks] Re: switch vs if -- which is faster

2010-11-22 Thread LALIT SHARMA
I was asking you for some examples ..as i dont have any knowledge
regarding this assembly code and how to study it  .

there was no intention of mine to challenge you or disregard you .

On 11/22/10, Gene gene.ress...@gmail.com wrote:
 I'm not sure what you mean by support.

 If you mean examples, gcc uses several of the techniques I mentioned.
 Write some switch statements with different distributions of cases.
 Compile with -S, and inspect the assembly code.  If you don't now how
 to do that, take the time to learn.

 If you mean justify the truth of what I wrote, it's observation while
 studying and teaching courses about compilers on and off for over 15
 years and building some myself.

 BTW, u c I won't ansr mor ??s if wrds arnt spld out...


 On Nov 22, 12:28 am, LALIT SHARMA lks.ru...@gmail.com wrote:
 @dipankar

 Can u tell us , sumthng more in support of ur ans...??

 On Mon, Nov 22, 2010 at 10:40 AM, DIPANKAR DUTTA

 dutta.dipanka...@gmail.com wrote:
  both are same .. because any good optimizing compiler generate same
  assembly
  code of them...

  On Mon, Nov 22, 2010 at 9:38 AM, Gene gene.ress...@gmail.com wrote:

  A good compiler will never generate a switch that is slower than an
  if...else chain.  It will analyze the switch cases and pick one of
  several options for code generation.  These may include cascaded
  conditional jumps (like if...else code), simple jump tables, segmented
  jump tables (cascaded jumps pick one of several jump tables because a
  single table would be too big), binary searches in a sorted table of
  case values, and perfect hash functions.  These are the ones I've
  seen.  There may be others.

  On Nov 21, 8:15 am, shiva shivanand.kadwad...@gmail.com wrote:
   As per my understanding it is compiler depending thing..

   what i feel is switch need to evaluate the expression only once but
   if
   else if need to evaluate the expression more than once(what if
   expression stored in variable and then compare...)

   Does any one please comment difference in speed of switch and if
   depending on how it is implemented...

   I heard switch uses jump table for its operation.

  --
  You received this message because you are subscribed to the Google
  Groups
  Algorithm Geeks group.
  To post to this group, send email to algoge...@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

  --
  DIPANKAR DUTTA
  M-TECH,Computer Science  Engg.
  EC Dept,IIT ROORKEE
  Uttarakhand , India – 247667
  ---
  website:http://people.iitr.ernet.in/shp/09535009/Website/index.html
  ph no-09045809987
  email:dipan...@iitr.ernet.in

  --
  You received this message because you are subscribed to the Google
  Groups
  Algorithm Geeks group.
  To post to this group, send email to algoge...@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

 --
 Lalit Kishore Sharma
 IIIT Allahabad (Amethi Capmus)
 5th Sem

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algoge...@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
Lalit Kishore Sharma

IIIT Allahabad (Amethi Capmus)
5th Sem

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: switch vs if -- which is faster

2010-11-21 Thread Chi
As he told you it depends on the data runs to the condition. If for
exampe statistically there is more of 1's then 0 then the switch loop
is faster, because:

1.) case is isolated
2.) You can reorder the case-statement according to the occurrent of
the conditions




On Nov 21, 2:59 pm, shiva shivanand.kadwad...@gmail.com wrote:
 how it going to make difference in following case
 --
 i=0;
 switch(i)
 {
 case 1: //some operation
 case 0:// some operation}

 --
 i=0;
 if(i==1)
 {
 //some operation;}

 else if (i==0)
 {
 //Some operation}

 --
 even compiler implement jump table like below

 key address
 1     x
 0     y

 Then two condition (worst case) need to be checked which is same as if
 else if

 Please correct me if i am wrong

 On Nov 21, 6:35 pm, MOHIT  mohit...@gmail.com wrote:

  for switch in the worst case compiler will generate if else chain .. else it
  uses binary decision tree or jump table for optimization at compile time .

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: switch vs if -- which is faster

2010-11-21 Thread MOHIT ....
its depend on compiler how it optimize it . he can make binary search tree
for this comparison
or perform cascading comparison as in if else case .. so worst case
complexity will be as of
if- else .

but for this case

switch(i)

{

case 4: //some operation

case 5:// some operation
case 6 : //
some operation

}
optimization can be done by compiler which need no comparison
just need to calculate index which need to comparison...

so in above case switch is better than if else.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: switch vs if -- which is faster

2010-11-21 Thread Chi
@Mohit:

No, it's just the hierarchical occurent of conditions that makes the
switch faster. I really doubt that a compiler build a binary search
tree for switches. Also, it a binary tree works for switches why it
can't optimize the if-else-condition? Did you make some testing?


On Nov 21, 3:28 pm, MOHIT  mohit...@gmail.com wrote:
 its depend on compiler how it optimize it . he can make binary search tree
 for this comparison
 or perform cascading comparison as in if else case .. so worst case
 complexity will be as of
 if- else .

 but for this case

 switch(i)

 {

 case 4: //some operation

 case 5:// some operation
 case 6 : //
 some operation

 }

 optimization can be done by compiler which need no comparison
 just need to calculate index which need to comparison...

 so in above case switch is better than if else.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: switch vs if -- which is faster

2010-11-21 Thread Gene
A good compiler will never generate a switch that is slower than an
if...else chain.  They will analyze the switch cases and pick one of
several options for code generation.  These may include cascaded
conditional jumps (like if...else code), simple jump tables, segmented
jump tables (cascaded jumps pick one of several jump tables because a
single table would be too big), binary searches in a sorted table of
case values, and perfect hash functions.  These are the ones I've
seen.  There may be others.


On Nov 21, 8:15 am, shiva shivanand.kadwad...@gmail.com wrote:
 As per my understanding it is compiler depending thing..

 what i feel is switch need to evaluate the expression only once but if
 else if need to evaluate the expression more than once(what if
 expression stored in variable and then compare...)

 Does any one please comment difference in speed of switch and if
 depending on how it is implemented...

 I heard switch uses jump table for its operation.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: switch vs if -- which is faster

2010-11-21 Thread DIPANKAR DUTTA
both are same .. because any good optimizing compiler generate same assembly
code of them...

On Mon, Nov 22, 2010 at 9:38 AM, Gene gene.ress...@gmail.com wrote:

 A good compiler will never generate a switch that is slower than an
 if...else chain.  They will analyze the switch cases and pick one of
 several options for code generation.  These may include cascaded
 conditional jumps (like if...else code), simple jump tables, segmented
 jump tables (cascaded jumps pick one of several jump tables because a
 single table would be too big), binary searches in a sorted table of
 case values, and perfect hash functions.  These are the ones I've
 seen.  There may be others.


 On Nov 21, 8:15 am, shiva shivanand.kadwad...@gmail.com wrote:
  As per my understanding it is compiler depending thing..
 
  what i feel is switch need to evaluate the expression only once but if
  else if need to evaluate the expression more than once(what if
  expression stored in variable and then compare...)
 
  Does any one please comment difference in speed of switch and if
  depending on how it is implemented...
 
  I heard switch uses jump table for its operation.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algoge...@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
DIPANKAR DUTTA
M-TECH,Computer Science  Engg.
EC Dept,IIT ROORKEE
Uttarakhand , India – 247667
---
website:http://people.iitr.ernet.in/shp/09535009/Website/index.html
ph no-09045809987
email:dipan...@iitr.ernet.in email%3adipan...@iitr.ernet.in

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: switch vs if -- which is faster

2010-11-21 Thread LALIT SHARMA
@dipankar

Can u tell us , sumthng more in support of ur ans...??


On Mon, Nov 22, 2010 at 10:40 AM, DIPANKAR DUTTA
dutta.dipanka...@gmail.com wrote:
 both are same .. because any good optimizing compiler generate same assembly
 code of them...

 On Mon, Nov 22, 2010 at 9:38 AM, Gene gene.ress...@gmail.com wrote:

 A good compiler will never generate a switch that is slower than an
 if...else chain.  They will analyze the switch cases and pick one of
 several options for code generation.  These may include cascaded
 conditional jumps (like if...else code), simple jump tables, segmented
 jump tables (cascaded jumps pick one of several jump tables because a
 single table would be too big), binary searches in a sorted table of
 case values, and perfect hash functions.  These are the ones I've
 seen.  There may be others.


 On Nov 21, 8:15 am, shiva shivanand.kadwad...@gmail.com wrote:
  As per my understanding it is compiler depending thing..
 
  what i feel is switch need to evaluate the expression only once but if
  else if need to evaluate the expression more than once(what if
  expression stored in variable and then compare...)
 
  Does any one please comment difference in speed of switch and if
  depending on how it is implemented...
 
  I heard switch uses jump table for its operation.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algoge...@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 DIPANKAR DUTTA
 M-TECH,Computer Science  Engg.
 EC Dept,IIT ROORKEE
 Uttarakhand , India – 247667
 ---
 website:http://people.iitr.ernet.in/shp/09535009/Website/index.html
 ph no-09045809987
 email:dipan...@iitr.ernet.in

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algoge...@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
Lalit Kishore Sharma
IIIT Allahabad (Amethi Capmus)
5th Sem

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: switch vs if -- which is faster

2010-11-21 Thread Gene
I'm not sure what you mean by support.

If you mean examples, gcc uses several of the techniques I mentioned.
Write some switch statements with different distributions of cases.
Compile with -S, and inspect the assembly code.  If you don't now how
to do that, take the time to learn.

If you mean justify the truth of what I wrote, it's observation while
studying and teaching courses about compilers on and off for over 15
years and building some myself.

BTW, u c I won't ansr mor ??s if wrds arnt spld out...


On Nov 22, 12:28 am, LALIT SHARMA lks.ru...@gmail.com wrote:
 @dipankar

 Can u tell us , sumthng more in support of ur ans...??

 On Mon, Nov 22, 2010 at 10:40 AM, DIPANKAR DUTTA

 dutta.dipanka...@gmail.com wrote:
  both are same .. because any good optimizing compiler generate same assembly
  code of them...

  On Mon, Nov 22, 2010 at 9:38 AM, Gene gene.ress...@gmail.com wrote:

  A good compiler will never generate a switch that is slower than an
  if...else chain.  It will analyze the switch cases and pick one of
  several options for code generation.  These may include cascaded
  conditional jumps (like if...else code), simple jump tables, segmented
  jump tables (cascaded jumps pick one of several jump tables because a
  single table would be too big), binary searches in a sorted table of
  case values, and perfect hash functions.  These are the ones I've
  seen.  There may be others.

  On Nov 21, 8:15 am, shiva shivanand.kadwad...@gmail.com wrote:
   As per my understanding it is compiler depending thing..

   what i feel is switch need to evaluate the expression only once but if
   else if need to evaluate the expression more than once(what if
   expression stored in variable and then compare...)

   Does any one please comment difference in speed of switch and if
   depending on how it is implemented...

   I heard switch uses jump table for its operation.

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algoge...@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

  --
  DIPANKAR DUTTA
  M-TECH,Computer Science  Engg.
  EC Dept,IIT ROORKEE
  Uttarakhand , India – 247667
  ---
  website:http://people.iitr.ernet.in/shp/09535009/Website/index.html
  ph no-09045809987
  email:dipan...@iitr.ernet.in

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algoge...@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

 --
 Lalit Kishore Sharma
 IIIT Allahabad (Amethi Capmus)
 5th Sem

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.