[nflug] PHP (switch vs. nested if)

Rob Dege livemotion at gmail.com
Thu Feb 21 15:08:48 EST 2008


nah, this won't work, since the arrays I'm referencing are already
two-dimensional arrays.  I mean I could create one large 2-dim array, and
pass that to each page every single time, but that would take up more memory
than it would need to.

Each 2-dim array has at least 5 entries.  So that would be over 400 entries
in on big array.  Seems bloated to load that big of an array when I only
need 5 entries from it.

That's why I decided a simple conditional statement would help me to break
down the array.  I figured the smaller the array, the faster the script
could process it, and the faster the page would load.  My only bottleneck
appeared to be the selection of the appropriate 2-dim array.

Sorry if I made the problem sound easier than what it was.  I wanted to
spare the pain of details :)

-Rob

On Thu, Feb 21, 2008 at 3:02 PM, Mark Musone <mmusone at shatterit.com> wrote:

>  The cleanest would be for your arrays to be a single array of arrays..
>
>
>
> For example:
>
> $myarray={1,2,3,4,5};
>
> $myotherarray={5,6,7,8,9};
>
>
>
> Would be a pain, but
>
>
>
> $arrays[0]={ 1,2,3,4,5};
>
> $arrays[1]={5,6,7,8,9};
>
>
>
>
>
> Then you simply have a single assignment:
>
>
>
> $myarraytouse=$array[$num];
>
>
>
>
>
> Mark
>
>
>
> *From:* nflug-bounces at nflug.org [mailto:nflug-bounces at nflug.org] *On
> Behalf Of *Rob Dege
> *Sent:* Thursday, February 21, 2008 2:57 PM
> *To:* nflug at nflug.org
>
> *Subject:* Re: [nflug] PHP (switch vs. nested if)
>
>
>
>
> I'm not took concerned about readability.  I already code in the style of
> spaghetti, so cleanliness is not my first concern.  I think I might break
> the switch into chunks of 20, and then use a single if statement to
> determine which switch to compare the variable against.
>
> Thanks for the replies though.
>
> Bob, I know not of this 'C' you language speak of... unless you forgot to
> add the '#' after it ;)
>
> -Rob
>
> On Thu, Feb 21, 2008 at 1:11 PM, Robert Meyer <meyer_rm at yahoo.com> wrote:
>
> Well, if you write the code in C, just create an array of addresses of
> arrays and just directly access the array by position.  Then you don't even
> need any comparisons. :-)
>
> Cheers!
>
> Bob
>
>
>
> --
> "When once you have tasted flight, you will forever walk the earth with
> your eyes turned skyward, for there you have been, and there you will always
> long to return."
> --Leonardo da Vinci
>
>
>
> ----- Original Message ----
> From: Franklin Kumro Jr <fkumro at gmail.com>
> To: nflug at nflug.org
> Sent: Thursday, February 21, 2008 12:18:07 PM
> Subject: Re: [nflug] PHP (switch vs. nested if)
>
> I would agree that a switch statement would be the preferred method.
> Multiple nested if statements hurt code readability, let alone a 1-80
> range.
>
> -Frank
> On Feb 21, 2008, at 11:43 AM, Timothy Finucane wrote:
>
> > A switch is the most efficient method. I'm not sure how much speed
> > performance is affected, but nested ifs would make the code
> > difficult to read.
> >
> > --
> > Timothy J. Finucane
> >
> > http://www.speljamr.com
> >
> > Facebook: http://www.facebook.com/p/Timothy_J_Finucane/682422189
> >
> >
> >
> > On 2/21/08, Rob Dege <livemotion at gmail.com> wrote:
> > Hi, I'm wondering which would provide faster processing time based
> > on the web page I'm building.
> >
> > I have a variable, let's call it $num.  $num is an int that can be
> > anywhere from 1 to 80.  Based on $num's value, an array is assigned
> > that is then used on the web page.  Since this is for a web page, I
> > want to minimize the wait time as much as possible, and I'm guessing
> > that this could be a potential bottleneck.  I have not written the
> > code yet, so I can't compare load times.  I'm considering this more
> > of a "do it the preferred way the first time".
> >
> > So I'm curious as to which would be faster, a simple switch with 80
> > possible cases, or a "nested if" that uses a sorting algorithm;  or
> > perhaps even more plausible, a combination of the two.
> >
> > Any random thoughts are appreciated :)
> >
> > --
> > -Rob
> >
> > Ben Franklin Quote: "They that can give up essential liberty to
> > obtain a little temporary safety deserve neither liberty nor safety."
> > _______________________________________________
> > nflug mailing list
> > nflug at nflug.org
> > http://www.nflug.org/mailman/listinfo/nflug
> >
> >
> >
> >
> >
> > _______________________________________________
> > nflug mailing list
> > nflug at nflug.org
> > http://www.nflug.org/mailman/listinfo/nflug
>
> _______________________________________________
> nflug mailing list
> nflug at nflug.org
> http://www.nflug.org/mailman/listinfo/nflug
>
>
>
>
>  ------------------------------
>
> Looking for last minute shopping deals? Find them fast with Yahoo! Search.<http://us.rd.yahoo.com/evt=51734/*http:/tools.search.yahoo.com/newsearch/category.php?category=shopping>
>
>
> _______________________________________________
> nflug mailing list
> nflug at nflug.org
> http://www.nflug.org/mailman/listinfo/nflug
>
>
>
>
> --
> -Rob
>
> Ben Franklin Quote: "They that can give up essential liberty to obtain a
> little temporary safety deserve neither liberty nor safety."
>
> _______________________________________________
> nflug mailing list
> nflug at nflug.org
> http://www.nflug.org/mailman/listinfo/nflug
>
>


-- 
-Rob

Ben Franklin Quote: "They that can give up essential liberty to obtain a
little temporary safety deserve neither liberty nor safety."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.nflug.org/pipermail/nflug/attachments/20080221/9cde971f/attachment-0001.html


More information about the nflug mailing list