home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18606 < prev    next >
Encoding:
Text File  |  1992-12-21  |  2.5 KB  |  75 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!pipex!demon!edscom!jim
  3. From: jim@edscom.demon.co.uk (Jim Thomas)
  4. Subject: Re: SWITCH Statement Question
  5. In-Reply-To: u-mwong%peruvian.cs.utah.edu@cs.utah.edu's message of 15 Dec 92 19:24:55 GMT
  6. Message-ID: <JIM.92Dec18125732@runningbear.edscom.demon.co.uk>
  7. X-Disclaimer: #include <std/witty/disclaimer.h>
  8. Lines: 59
  9. Sender: jim@edscom.demon.co.uk (Jim Thomas)
  10. Reply-To: Jim Thomas <jthomas@edscom.demon.co.uk>
  11. Organization: EDS-Scicon, Milton Keynes, UK
  12. References: <1992Dec15.122456.1776@hellgate.utah.edu>
  13. Distribution: usa
  14. Date: Fri, 18 Dec 1992 12:57:35 GMT
  15.  
  16. >>>>> On 15 Dec 92 19:24:55 GMT, u-mwong%peruvian.cs.utah.edu@cs.utah.edu (Michael Wong) said:
  17.  
  18.  
  19. >>    I have a question regarding the SWITCH statement in C.  I'm having
  20. >> conflicting answers from people at work and need some help.  How does
  21. >> the SWITCH statement process the CASE statements?  It's not clear to
  22. >> me how it works internally.  The following example will help clarify
  23. >> my question...
  24.  
  25. >>    Will using a SWITCH statement run faster if it had few CASE
  26. >> statements and IF statements were used to aid in the processing than
  27. >> using a SWITCH statement that had a very long list of CASE
  28. >> statements.  In other words, compared to:
  29.  
  30. >> switch (key) { <50 CASE statements here> }
  31.  
  32. >> would this following code be faster, if the SWITCH was broken down
  33. >> using IFs:
  34.  
  35. >> if (...)
  36. >> {
  37. >>   switch (key)
  38. >>   {
  39. >>     <10 CASE statements here>
  40. >>   }
  41. >> }
  42. >> else if (...)
  43. >> {
  44. >>   switch (key)
  45. >>   {
  46. >>     <10 CASE statements here>
  47. >>   }
  48. >> }
  49. >> else if (...)
  50. >>   <more SWITCH statements>
  51.  
  52.  
  53. >>    I'm worried about speed, but I also want to keep the code easy to
  54. >> understand.  I'd appreciate if someone could tell me exactly how SWITCH
  55. >> works internally!
  56.  
  57. This is all compiler specific. I use Microsoft C and it handles switch
  58. statements very effeciently, building a combination of jumps and small
  59. sparse jump tables as required by the kind of cases you use. 
  60.  
  61. I have found that unless you know a lot about how the compiler works it
  62. is never a good idea to try and second guess it. If you add code to make
  63. it more effecient you may make it harder for the compiler to optimise
  64. your code.
  65.  
  66. If speed or size is a problem after implementation then maybe look at
  67. stuff like this but otherwise try to make the code as readable as
  68. possible.
  69.  
  70.  
  71. --
  72. Jim Thomas, EDS-Scicon Ltd,Wavendon,Wavendon Tower,Milton Keynes,MK17 8LX,UK
  73. jim@edscom.demon.co.uk   Tel:  +44 908 284522
  74. Opinions expressed are my own, and are not necessarily those of my employer.
  75.