www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/27/15:06:45

Message-ID: <19980927190639.12830.qmail@hotmail.com>
From: "Oliver Richman" <orichman AT hotmail DOT com>
To: craig AT cdworld DOT co DOT uk
Cc: djgpp AT delorie DOT com
Subject: Re: fast way to do a if statement -- the ultimate solution
Date: Sun, 27 Sep 1998 12:06:39 PDT

>I have a little program I have
>writen to decode something but
>most of the program is taken up
>with:
>
>if (x==1) do...
>if (x==2) do...
>if (x==3) do...
>if (x==4) do...
>if (x==5) do...
>etc...
>That goes on for about 40 lines.
>Is there a way to do it quicker
>that would mean less messing
>about for the CPU?

Without question, the best tradeoff between ultimate speed and ultimate 
code organization in C/C++ for such a scenatio, is to rewrite your code 
thusly:

1. Functionize all the if procesures.
2. Build an array of pointers to function organized by #, such that if 
you wanted to call the function for X==1, funclist[1] would be *f().
3. Do this: "f[X](args);". boom!
4. or something like this, i've never actually needed to go this far 
yet. But I have seen it done.

--opr

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019