home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18421 < prev    next >
Encoding:
Internet Message Format  |  1992-12-15  |  1.4 KB

  1. Path: sparky!uunet!olivea!spool.mu.edu!agate!dog.ee.lbl.gov!hellgate.utah.edu!peruvian.cs.utah.edu!u-mwong
  2. From: u-mwong%peruvian.cs.utah.edu@cs.utah.edu (Michael Wong)
  3. Newsgroups: comp.lang.c
  4. Subject: SWITCH Statement Question
  5. Message-ID: <1992Dec15.122456.1776@hellgate.utah.edu>
  6. Date: 15 Dec 92 19:24:55 GMT
  7. Distribution: usa
  8. Organization: University of Utah CS Dept
  9. Lines: 45
  10.  
  11.  
  12.    I have a question regarding the SWITCH statement in C.  I'm having
  13. conflicting answers from people at work and need some help.  How does the
  14. SWITCH statement process the CASE statements?  It's not clear to me how
  15. it works internally.  The following example will help clarify my question...
  16.  
  17.    Will using a SWITCH statement run faster if it had few CASE statements and 
  18. IF statements were used to aid in the processing than using a SWITCH 
  19. statement that had a very long list of CASE statements.  In other words, 
  20. compared to:
  21.  
  22. switch (key)
  23. {
  24.   <50 CASE statements here>
  25. }
  26.  
  27. would this following code be faster, if the SWITCH was broken down using IFs:
  28.  
  29. if (...)
  30. {
  31.   switch (key)
  32.   {
  33.     <10 CASE statements here>
  34.   }
  35. }
  36. else if (...)
  37. {
  38.   switch (key)
  39.   {
  40.     <10 CASE statements here>
  41.   }
  42. }
  43. else if (...)
  44.   <more SWITCH statements>
  45.  
  46.  
  47.    I'm worried about speed, but I also want to keep the code easy to
  48. understand.  I'd appreciate if someone could tell me exactly how SWITCH
  49. works internally!
  50.  
  51. -Mike
  52. ----------
  53. Michael Wong 
  54. u-mwong@peruvian.utah.edu
  55. University of Utah
  56.