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

  1. Path: sparky!uunet!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
  2. From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: SWITCH Statement Question
  5. Message-ID: <1992Dec17.221738.7740@thunder.mcrcim.mcgill.edu>
  6. Date: 17 Dec 92 22:17:38 GMT
  7. References: <1992Dec15.122456.1776@hellgate.utah.edu>
  8. Distribution: na
  9. Organization: McGill Research Centre for Intelligent Machines
  10. Lines: 51
  11.  
  12. > Distribution: usa
  13.  
  14. Sigh.  I'm going to have to play with some scripts and complain to our
  15. newsfeeds about their passing us bogus distributions...
  16.  
  17. In article <1992Dec15.122456.1776@hellgate.utah.edu>, u-mwong%peruvian.cs.utah.edu@cs.utah.edu (Michael Wong) writes:
  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.
  23.  
  24. It varies, depending on machine, system, compiler, the sparsity of the
  25. labels, and possibly other factors such as optimization tradeoff
  26. options specified at compile time.
  27.  
  28. > Will using a SWITCH statement run faster if it had few CASE
  29. > statements and IF statements were used to aid in the processing than
  30. > using a SWITCH statement that had a very long list of CASE
  31. > statements[?]
  32.  
  33. Maybe.  It depends on machine, system, compiler, the sparsity of the
  34. labels, and possibly other factors such as optimization tradeoff
  35. options specified at compile time.
  36.  
  37. A good compiler, told to optimize for speed, should make this
  38. transformation automatically if it would speed up the resulting code
  39. without changing the semantics.
  40.  
  41. > I'm worried about speed, but I also want to keep the code easy to
  42. > understand.
  43.  
  44. Then write it whatever way is clearest.  If the time cost of the switch
  45. turns out to be significant (not "is thought to perhaps maybe be
  46. significant"), and the compiler you're using doesn't make simple
  47. transformations that would speed it up, then (a) start looking for a
  48. better compiler and (b) put in some sort of system-specific conditional
  49. compilation in the meantime.
  50.  
  51. > I'd appreciate if someone could tell me exactly how SWITCH works
  52. > internally!
  53.  
  54. Go read your compiler documentation, or failing that source, or failing
  55. that go ask your compiler vendor.  If you want to know about some
  56. specific example, look at the generated code, but be careful not to
  57. jump to conclusions.
  58.  
  59.                     der Mouse
  60.  
  61.                 mouse@larry.mcrcim.mcgill.edu
  62.