|
List Info
Thread: Re: Simplifying a basic if statement conditional
|
|
| Re: Simplifying a basic if statement
conditional |
  United States |
2007-05-23 05:18:29 |
On May 23, 10:18 am, "Stas Trefilov"
<stas.trefi... gmail.com> wrote:
> once again, ian, no one was talking here about
readability or
> maintainability. best practices may be good to know for
classical
> situations but the question here was about
effectiveness -- something
> that is not on mind of most classical developers.
>
> and yes, this is php forum. we don't count chances, we
solve real problems.
>
> On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
>
>
>
> > Stas,
>
> > No-one is saying that it isn't possible that in
extreme conditions there may
> > be an argument for speed over readability. In a
previous post I said "Unless
> > you are in a tight loop, the array solution will
have no discernable effect
> > on speed". I would consider loading a 2gb csv
file into a database a 'tight
> > loop'.
>
> > If loading 20 million records or thereabouts is a
regular feature of a
> > system and users are being forced to wait while
they are loaded, then there
> > is something seriously wrong with the design of
the system.
>
> > I am trying to talk about general programming good
practice, and of course
> > it will be possible to come up with extreme
examples where general
> > principles should be abandonded in the interests
of some other
> > consideration. There are times when the fastest
data transfer method is to
> > put 10 boxes of DVDs in a taxi, but it is not the
best way to transfer data
> > under most conditions.
>
> > This is a PHP forum. The chances are that the OP
is developing a web
> > application. The chances are also in favour of the
OP's program is not
> > attempting to load 20m records from a csv file
into a database every time
> > any user anywhere in the world hits Enter. The
chances are that the OP just
> > wants to check a condition.
>
> > In these overwhemingly likely circumstances it is
my opinion that the OP
> > should go for readability rather than try to save
3 nanoseconds from a call
> > which is going to take at least a second over the
net.
>
> > If you (or anyone else) believes that your
solution is more readable and
> > more maintainable, then of course you should
follow your instincts. If that
> > is the case, then I consider you a truely
exceptional person, and I hope you
> > have the sort of luck which will get you nothing
but deeply-nested,
> > unstructured, monolithic blocks of code for the
rest of your life. Someone
> > has to do it, and I'd rather it was you than me
>
> > Ian
>
> > On 23/05/07, stas <stas.trefi... gmail.com> wrote:
>
> > > denis, ian
>
> > > just think about other systems than big
commercial software. please! i
> > > know you have a good imagination. consider
this: a php script receives
> > > a giant text file (say 2 gb) with a csv data
which needs to be parsed
> > > and only load into a database the lines where
the column x has one of
> > > constant values. thanks denis we know that
the switch statement will
> > > take 3 to 7 times faster than in_array () and
much more faster than a
> > > function call. the question now: why should
we care about best
> > > practices of enterprise application
development for the script where
> > > the speed (efficiency) is crucial? why should
we follow these j2ee
> > > guidelines here? you say because it's more
readable. i say ask your
> > > user waiting for the file to be processed.
>
> > > --
> > > free web developer tools (now with sitemap
support):
> > >http://osbtools.goo
glepages.com/- Hide quoted text -
>
> - Show quoted text -
Hi Fellas,
Thanks a lot for the replies on this. You seem to have
started an
interesting debate! I'm not sure if it really matters at
this point
but for the record, I am developing a web application but it
is not
importing any 2GB text files!! I was simply looking for an
easier/
quicker way to write a conditional like the one above as I
was finding
myself typing long messy statements like that quite often. I
was in
fact, hoping there was a function in PHP that emulates the
WHERE x
IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently not! I
guess
inarray() is the closest thing, however if switch is faster,
perhaps
it would be feasible to write a universal function that
crosschecks a
group of passed vars using switch? Would something like that
be
noticeably slower than the standard method in my original
post? I'm
not overly concerned with speed, I mean it will only be a
max of 10
checks at a time anyway. Im not even sure how to send an
indefinate
number of paramaters to a function?
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |
  United States |
2007-05-23 05:35:53 |
next time be more precise in your questions if you don't
want to be a
cause of holy wars
in fact switch is only faster if you have a constant number
of values.
if your values vary then put them in an array and use
in_array
($vars). don't forget to set some meaningful name for your
array. try
not to write functions as simple wrappers around core php
functions,
reuse them!
to pass unlimited number of parameters into your function
use hash
array like this:
<?php
function func ($params)
{
$var = $param['var1'];
echo $var1;
}
func (array ('var1'=>'this is a value for var1'
, 'var2'=>'this is a value for var2'
));
?>
--
free web developer tools (now with sitemap support):
http://osbtools.goog
lepages.com/
On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> On May 23, 10:18 am, "Stas Trefilov"
<stas.trefi... gmail.com> wrote:
>
>
>
> > once again, ian, no one was talking here about
readability or
> > maintainability. best practices may be good to
know for classical
> > situations but the question here was about
effectiveness -- something
> > that is not on mind of most classical developers.
>
> > and yes, this is php forum. we don't count
chances, we solve real problems.
>
> > On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
>
> > > Stas,
>
> > > No-one is saying that it isn't possible that
in extreme conditions there may
> > > be an argument for speed over readability. In
a previous post I said "Unless
> > > you are in a tight loop, the array solution
will have no discernable effect
> > > on speed". I would consider loading a
2gb csv file into a database a 'tight
> > > loop'.
>
> > > If loading 20 million records or thereabouts
is a regular feature of a
> > > system and users are being forced to wait
while they are loaded, then there
> > > is something seriously wrong with the design
of the system.
>
> > > I am trying to talk about general programming
good practice, and of course
> > > it will be possible to come up with extreme
examples where general
> > > principles should be abandonded in the
interests of some other
> > > consideration. There are times when the
fastest data transfer method is to
> > > put 10 boxes of DVDs in a taxi, but it is not
the best way to transfer data
> > > under most conditions.
>
> > > This is a PHP forum. The chances are that the
OP is developing a web
> > > application. The chances are also in favour
of the OP's program is not
> > > attempting to load 20m records from a csv
file into a database every time
> > > any user anywhere in the world hits Enter.
The chances are that the OP just
> > > wants to check a condition.
>
> > > In these overwhemingly likely circumstances
it is my opinion that the OP
> > > should go for readability rather than try to
save 3 nanoseconds from a call
> > > which is going to take at least a second over
the net.
>
> > > If you (or anyone else) believes that your
solution is more readable and
> > > more maintainable, then of course you should
follow your instincts. If that
> > > is the case, then I consider you a truely
exceptional person, and I hope you
> > > have the sort of luck which will get you
nothing but deeply-nested,
> > > unstructured, monolithic blocks of code for
the rest of your life. Someone
> > > has to do it, and I'd rather it was you than
me
>
> > > Ian
>
> > > On 23/05/07, stas <stas.trefi... gmail.com> wrote:
>
> > > > denis, ian
>
> > > > just think about other systems than big
commercial software. please! i
> > > > know you have a good imagination.
consider this: a php script receives
> > > > a giant text file (say 2 gb) with a csv
data which needs to be parsed
> > > > and only load into a database the lines
where the column x has one of
> > > > constant values. thanks denis we know
that the switch statement will
> > > > take 3 to 7 times faster than in_array
() and much more faster than a
> > > > function call. the question now: why
should we care about best
> > > > practices of enterprise application
development for the script where
> > > > the speed (efficiency) is crucial? why
should we follow these j2ee
> > > > guidelines here? you say because it's
more readable. i say ask your
> > > > user waiting for the file to be
processed.
>
> > > > --
> > > > free web developer tools (now with
sitemap support):
> > > >http://osbtools
.googlepages.com/-Hide quoted text -
>
> > - Show quoted text -
>
> Hi Fellas,
> Thanks a lot for the replies on this. You seem to have
started an
> interesting debate! I'm not sure if it really matters
at this point
> but for the record, I am developing a web application
but it is not
> importing any 2GB text files!! I was simply looking for
an easier/
> quicker way to write a conditional like the one above
as I was finding
> myself typing long messy statements like that quite
often. I was in
> fact, hoping there was a function in PHP that emulates
the WHERE x
> IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently not! I
guess
> inarray() is the closest thing, however if switch is
faster, perhaps
> it would be feasible to write a universal function that
crosschecks a
> group of passed vars using switch? Would something like
that be
> noticeably slower than the standard method in my
original post? I'm
> not overly concerned with speed, I mean it will only be
a max of 10
> checks at a time anyway. Im not even sure how to send
an indefinate
> number of paramaters to a function?
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |
  United States |
2007-05-23 05:36:30 |
next time be more precise in your questions if you don't
want to be a
cause of holy wars
in fact switch is only faster if you have a constant number
of values.
if your values vary then put them in an array and use
in_array
($vars). don't forget to set some meaningful name for your
array. try
not to write functions as simple wrappers around core php
functions,
reuse them!
to pass unlimited number of parameters into your function
use hash
array like this:
<?php
function func ($params)
{
$var = $param['var1'];
echo $var1;
}
func (array ('var1'=>'this is a value for var1'
, 'var2'=>'this is a value for var2'
));
?>
--
free web developer tools (now with sitemap support):
http://osbtools.goog
lepages.com/
On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> On May 23, 10:18 am, "Stas Trefilov"
<stas.trefi... gmail.com> wrote:
>
>
>
> > once again, ian, no one was talking here about
readability or
> > maintainability. best practices may be good to
know for classical
> > situations but the question here was about
effectiveness -- something
> > that is not on mind of most classical developers.
>
> > and yes, this is php forum. we don't count
chances, we solve real problems.
>
> > On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
>
> > > Stas,
>
> > > No-one is saying that it isn't possible that
in extreme conditions there may
> > > be an argument for speed over readability. In
a previous post I said "Unless
> > > you are in a tight loop, the array solution
will have no discernable effect
> > > on speed". I would consider loading a
2gb csv file into a database a 'tight
> > > loop'.
>
> > > If loading 20 million records or thereabouts
is a regular feature of a
> > > system and users are being forced to wait
while they are loaded, then there
> > > is something seriously wrong with the design
of the system.
>
> > > I am trying to talk about general programming
good practice, and of course
> > > it will be possible to come up with extreme
examples where general
> > > principles should be abandonded in the
interests of some other
> > > consideration. There are times when the
fastest data transfer method is to
> > > put 10 boxes of DVDs in a taxi, but it is not
the best way to transfer data
> > > under most conditions.
>
> > > This is a PHP forum. The chances are that the
OP is developing a web
> > > application. The chances are also in favour
of the OP's program is not
> > > attempting to load 20m records from a csv
file into a database every time
> > > any user anywhere in the world hits Enter.
The chances are that the OP just
> > > wants to check a condition.
>
> > > In these overwhemingly likely circumstances
it is my opinion that the OP
> > > should go for readability rather than try to
save 3 nanoseconds from a call
> > > which is going to take at least a second over
the net.
>
> > > If you (or anyone else) believes that your
solution is more readable and
> > > more maintainable, then of course you should
follow your instincts. If that
> > > is the case, then I consider you a truely
exceptional person, and I hope you
> > > have the sort of luck which will get you
nothing but deeply-nested,
> > > unstructured, monolithic blocks of code for
the rest of your life. Someone
> > > has to do it, and I'd rather it was you than
me
>
> > > Ian
>
> > > On 23/05/07, stas <stas.trefi... gmail.com> wrote:
>
> > > > denis, ian
>
> > > > just think about other systems than big
commercial software. please! i
> > > > know you have a good imagination.
consider this: a php script receives
> > > > a giant text file (say 2 gb) with a csv
data which needs to be parsed
> > > > and only load into a database the lines
where the column x has one of
> > > > constant values. thanks denis we know
that the switch statement will
> > > > take 3 to 7 times faster than in_array
() and much more faster than a
> > > > function call. the question now: why
should we care about best
> > > > practices of enterprise application
development for the script where
> > > > the speed (efficiency) is crucial? why
should we follow these j2ee
> > > > guidelines here? you say because it's
more readable. i say ask your
> > > > user waiting for the file to be
processed.
>
> > > > --
> > > > free web developer tools (now with
sitemap support):
> > > >http://osbtools
.googlepages.com/-Hide quoted text -
>
> > - Show quoted text -
>
> Hi Fellas,
> Thanks a lot for the replies on this. You seem to have
started an
> interesting debate! I'm not sure if it really matters
at this point
> but for the record, I am developing a web application
but it is not
> importing any 2GB text files!! I was simply looking for
an easier/
> quicker way to write a conditional like the one above
as I was finding
> myself typing long messy statements like that quite
often. I was in
> fact, hoping there was a function in PHP that emulates
the WHERE x
> IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently not! I
guess
> inarray() is the closest thing, however if switch is
faster, perhaps
> it would be feasible to write a universal function that
crosschecks a
> group of passed vars using switch? Would something like
that be
> noticeably slower than the standard method in my
original post? I'm
> not overly concerned with speed, I mean it will only be
a max of 10
> checks at a time anyway. Im not even sure how to send
an indefinate
> number of paramaters to a function?
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-23 06:15:21 |
Someone (*poke*, Stas) does not know PHP properly ^_^
function in()
{
$list = func_get_args();
$item = array_shift($list);
return in_array($item, $list);
}
if (in($number,1,3,5,17)) do_foo();
^_~
On 5/23/07, stas <stas.trefilov gmail.com> wrote:
>
> next time be more precise in your questions if you
don't want to be a
> cause of holy wars
>
> in fact switch is only faster if you have a constant
number of values.
> if your values vary then put them in an array and use
in_array
> ($vars). don't forget to set some meaningful name for
your array. try
> not to write functions as simple wrappers around core
php functions,
> reuse them!
>
> to pass unlimited number of parameters into your
function use hash
> array like this:
>
> <?php
> function func ($params)
> {
> $var = $param['var1'];
> echo $var1;
> }
>
> func (array ('var1'=>'this is a value for var1'
> , 'var2'=>'this is a value for var2'
> ));
> ?>
>
> --
> free web developer tools (now with sitemap support):
> http://osbtools.goog
lepages.com/
>
>
> On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> > On May 23, 10:18 am, "Stas Trefilov"
<stas.trefi... gmail.com> wrote:
> >
> >
> >
> > > once again, ian, no one was talking here
about readability or
> > > maintainability. best practices may be good
to know for classical
> > > situations but the question here was about
effectiveness -- something
> > > that is not on mind of most classical
developers.
> >
> > > and yes, this is php forum. we don't count
chances, we solve real problems.
> >
> > > On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
> >
> > > > Stas,
> >
> > > > No-one is saying that it isn't possible
that in extreme conditions there may
> > > > be an argument for speed over
readability. In a previous post I said "Unless
> > > > you are in a tight loop, the array
solution will have no discernable effect
> > > > on speed". I would consider loading
a 2gb csv file into a database a 'tight
> > > > loop'.
> >
> > > > If loading 20 million records or
thereabouts is a regular feature of a
> > > > system and users are being forced to
wait while they are loaded, then there
> > > > is something seriously wrong with the
design of the system.
> >
> > > > I am trying to talk about general
programming good practice, and of course
> > > > it will be possible to come up with
extreme examples where general
> > > > principles should be abandonded in the
interests of some other
> > > > consideration. There are times when the
fastest data transfer method is to
> > > > put 10 boxes of DVDs in a taxi, but it
is not the best way to transfer data
> > > > under most conditions.
> >
> > > > This is a PHP forum. The chances are
that the OP is developing a web
> > > > application. The chances are also in
favour of the OP's program is not
> > > > attempting to load 20m records from a
csv file into a database every time
> > > > any user anywhere in the world hits
Enter. The chances are that the OP just
> > > > wants to check a condition.
> >
> > > > In these overwhemingly likely
circumstances it is my opinion that the OP
> > > > should go for readability rather than
try to save 3 nanoseconds from a call
> > > > which is going to take at least a second
over the net.
> >
> > > > If you (or anyone else) believes that
your solution is more readable and
> > > > more maintainable, then of course you
should follow your instincts. If that
> > > > is the case, then I consider you a
truely exceptional person, and I hope you
> > > > have the sort of luck which will get you
nothing but deeply-nested,
> > > > unstructured, monolithic blocks of code
for the rest of your life. Someone
> > > > has to do it, and I'd rather it was you
than me
> >
> > > > Ian
> >
> > > > On 23/05/07, stas <stas.trefi... gmail.com> wrote:
> >
> > > > > denis, ian
> >
> > > > > just think about other systems than
big commercial software. please! i
> > > > > know you have a good imagination.
consider this: a php script receives
> > > > > a giant text file (say 2 gb) with a
csv data which needs to be parsed
> > > > > and only load into a database the
lines where the column x has one of
> > > > > constant values. thanks denis we
know that the switch statement will
> > > > > take 3 to 7 times faster than
in_array () and much more faster than a
> > > > > function call. the question now:
why should we care about best
> > > > > practices of enterprise application
development for the script where
> > > > > the speed (efficiency) is crucial?
why should we follow these j2ee
> > > > > guidelines here? you say because
it's more readable. i say ask your
> > > > > user waiting for the file to be
processed.
> >
> > > > > --
> > > > > free web developer tools (now with
sitemap support):
> > > > >http://osbtools
.googlepages.com/-Hide quoted text -
> >
> > > - Show quoted text -
> >
> > Hi Fellas,
> > Thanks a lot for the replies on this. You seem to
have started an
> > interesting debate! I'm not sure if it really
matters at this point
> > but for the record, I am developing a web
application but it is not
> > importing any 2GB text files!! I was simply
looking for an easier/
> > quicker way to write a conditional like the one
above as I was finding
> > myself typing long messy statements like that
quite often. I was in
> > fact, hoping there was a function in PHP that
emulates the WHERE x
> > IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently
not! I guess
> > inarray() is the closest thing, however if switch
is faster, perhaps
> > it would be feasible to write a universal function
that crosschecks a
> > group of passed vars using switch? Would something
like that be
> > noticeably slower than the standard method in my
original post? I'm
> > not overly concerned with speed, I mean it will
only be a max of 10
> > checks at a time anyway. Im not even sure how to
send an indefinate
> > number of paramaters to a function?
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-23 06:24:08 |
P.S. Ran a few benchmarks.
function in()
{
$list = func_get_args();
$item = array_shift($list);
return in_array($item, $list);
}
------way 1-------
if (in($number,1,3,5,17)) true;
------/way 1-------
------way 2-------
if (in_array($number, array(1,3,5,17))) true;
------/way 2-------
Way 1 is only 2-3 times slower, and that's a low price to
pay for
beauty, isn't it?
Though I still prefer way 3:
------way 3-------
if (valid_number($number)) true;
------/way 3-------
Where valid_number() is
function valid_number($no)
{
return in_array($no, GOOD_VALUES);
}
define('GOOD_VALUE',array(1,2,43));
Which is portable, changeable and 3 times faster than way 1.
Yet if
good_values is dynamic, then way 1 is better. Well, it's all
up to
needs.
On 5/23/07, Denis <sokolov.denis gmail.com> wrote:
> Someone (*poke*, Stas) does not know PHP properly ^_^
>
> function in()
> {
> $list = func_get_args();
> $item = array_shift($list);
> return in_array($item, $list);
> }
>
> if (in($number,1,3,5,17)) do_foo();
>
> ^_~
>
>
> On 5/23/07, stas <stas.trefilov gmail.com> wrote:
> >
> > next time be more precise in your questions if you
don't want to be a
> > cause of holy wars
> >
> > in fact switch is only faster if you have a
constant number of values.
> > if your values vary then put them in an array and
use in_array
> > ($vars). don't forget to set some meaningful name
for your array. try
> > not to write functions as simple wrappers around
core php functions,
> > reuse them!
> >
> > to pass unlimited number of parameters into your
function use hash
> > array like this:
> >
> > <?php
> > function func ($params)
> > {
> > $var = $param['var1'];
> > echo $var1;
> > }
> >
> > func (array ('var1'=>'this is a value for
var1'
> > , 'var2'=>'this is a value for var2'
> > ));
> > ?>
> >
> > --
> > free web developer tools (now with sitemap
support):
> > http://osbtools.goog
lepages.com/
> >
> >
> > On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> > > On May 23, 10:18 am, "Stas
Trefilov" <stas.trefi... gmail.com> wrote:
> > >
> > >
> > >
> > > > once again, ian, no one was talking here
about readability or
> > > > maintainability. best practices may be
good to know for classical
> > > > situations but the question here was
about effectiveness -- something
> > > > that is not on mind of most classical
developers.
> > >
> > > > and yes, this is php forum. we don't
count chances, we solve real problems.
> > >
> > > > On 5/23/07, Ian Bambury
<ianbamb... gmail.com> wrote:
> > >
> > > > > Stas,
> > >
> > > > > No-one is saying that it isn't
possible that in extreme conditions there may
> > > > > be an argument for speed over
readability. In a previous post I said "Unless
> > > > > you are in a tight loop, the array
solution will have no discernable effect
> > > > > on speed". I would consider
loading a 2gb csv file into a database a 'tight
> > > > > loop'.
> > >
> > > > > If loading 20 million records or
thereabouts is a regular feature of a
> > > > > system and users are being forced
to wait while they are loaded, then there
> > > > > is something seriously wrong with
the design of the system.
> > >
> > > > > I am trying to talk about general
programming good practice, and of course
> > > > > it will be possible to come up with
extreme examples where general
> > > > > principles should be abandonded in
the interests of some other
> > > > > consideration. There are times when
the fastest data transfer method is to
> > > > > put 10 boxes of DVDs in a taxi, but
it is not the best way to transfer data
> > > > > under most conditions.
> > >
> > > > > This is a PHP forum. The chances
are that the OP is developing a web
> > > > > application. The chances are also
in favour of the OP's program is not
> > > > > attempting to load 20m records from
a csv file into a database every time
> > > > > any user anywhere in the world hits
Enter. The chances are that the OP just
> > > > > wants to check a condition.
> > >
> > > > > In these overwhemingly likely
circumstances it is my opinion that the OP
> > > > > should go for readability rather
than try to save 3 nanoseconds from a call
> > > > > which is going to take at least a
second over the net.
> > >
> > > > > If you (or anyone else) believes
that your solution is more readable and
> > > > > more maintainable, then of course
you should follow your instincts. If that
> > > > > is the case, then I consider you a
truely exceptional person, and I hope you
> > > > > have the sort of luck which will
get you nothing but deeply-nested,
> > > > > unstructured, monolithic blocks of
code for the rest of your life. Someone
> > > > > has to do it, and I'd rather it was
you than me
> > >
> > > > > Ian
> > >
> > > > > On 23/05/07, stas
<stas.trefi... gmail.com> wrote:
> > >
> > > > > > denis, ian
> > >
> > > > > > just think about other systems
than big commercial software. please! i
> > > > > > know you have a good
imagination. consider this: a php script receives
> > > > > > a giant text file (say 2 gb)
with a csv data which needs to be parsed
> > > > > > and only load into a database
the lines where the column x has one of
> > > > > > constant values. thanks denis
we know that the switch statement will
> > > > > > take 3 to 7 times faster than
in_array () and much more faster than a
> > > > > > function call. the question
now: why should we care about best
> > > > > > practices of enterprise
application development for the script where
> > > > > > the speed (efficiency) is
crucial? why should we follow these j2ee
> > > > > > guidelines here? you say
because it's more readable. i say ask your
> > > > > > user waiting for the file to
be processed.
> > >
> > > > > > --
> > > > > > free web developer tools (now
with sitemap support):
> > > > > >http://osbtools
.googlepages.com/-Hide quoted text -
> > >
> > > > - Show quoted text -
> > >
> > > Hi Fellas,
> > > Thanks a lot for the replies on this. You
seem to have started an
> > > interesting debate! I'm not sure if it really
matters at this point
> > > but for the record, I am developing a web
application but it is not
> > > importing any 2GB text files!! I was simply
looking for an easier/
> > > quicker way to write a conditional like the
one above as I was finding
> > > myself typing long messy statements like that
quite often. I was in
> > > fact, hoping there was a function in PHP that
emulates the WHERE x
> > > IN(1,3,4,5,6,7,9,etc) syntax in mySql.
Evidently not! I guess
> > > inarray() is the closest thing, however if
switch is faster, perhaps
> > > it would be feasible to write a universal
function that crosschecks a
> > > group of passed vars using switch? Would
something like that be
> > > noticeably slower than the standard method in
my original post? I'm
> > > not overly concerned with speed, I mean it
will only be a max of 10
> > > checks at a time anyway. Im not even sure how
to send an indefinate
> > > number of paramaters to a function?
> >
> >
> > > >
> >
>
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |
  France |
2007-05-23 06:51:30 |
|
| denis, you make common mistakes which disclose a very young and unexperienced person behind your nick. don't judge other's ability in programming languages by the fact that the person does not use func_get_args. don't substitute a simple function call to a core function by 3 core function calls and one user function call. don't use unneded wrappers.
good luck on your hard way. Le 23 mai 07 à 13:15, Denis a écrit :
Someone (*poke*, Stas) does not know PHP properly ^_^
function in() { $list = func_get_args(); $item = array_shift($list); return in_array($item, $list); }
if (in($number,1,3,5,17)) do_foo();
^_~
On 5/23/07, stas < stas.trefi lov gmail.com">stas.trefilov gmail.com> wrote:
next time be more precise in your questions if you don't want to be a cause of holy wars 
in fact switch is only faster if you have a constant number of values. if your values vary then put them in an array and use in_array ($vars). don't forget to set some meaningful name for your array. try not to write functions as simple wrappers around core php functions, reuse them!
to pass unlimited number of parameters into your function use hash array like this:
<?php function func ($params) { $var = $param['var1']; echo $var1; }
func (array ('var1'=>'this is a value for var1' , 'var2'=>'this is a value for var2' )); ?>
-- free web developer tools (now with sitemap support):
On May 23, 12:18 pm, Ciaran <cronok ... hotmail.com> wrote: On May 23, 10:18 am, "Stas Trefilov" <stas.t refi... gmail.com> wrote:
once again, ian, no one was talking here about readability or maintainability. best practices may be good to know for classical situations but the question here was about effectiveness -- something that is not on mind of most classical developers.
and yes, this is php forum. we don't count chances, we solve real problems.
On 5/23/07, Ian Bambury <ianbam b... gmail.com> wrote:
Stas,
No-one is saying that it isn't possible that in extreme conditions there may be an argument for speed over readability. In a previous post I said "Unless you are in a tight loop, the array solution will have no discernable effect on speed". I would consider loading a 2gb csv file into a database a 'tight loop'.
If loading 20 million records or thereabouts is a regular feature of a system and users are being forced to wait while they are loaded, then there is something seriously wrong with the design of the system.
I am trying to talk about general programming good practice, and of course it will be possible to come up with extreme examples where general principles should be abandonded in the interests of some other consideration. There are times when the fastest data transfer method is to put 10 boxes of DVDs in a taxi, but it is not the best way to transfer data under most conditions.
This is a PHP forum. The chances are that the OP is developing a web application. The chances are also in favour of the OP's program is not attempting to load 20m records from a csv file into a database every time any user anywhere in the world hits Enter. The chances are that the OP just wants to check a condition.
In these overwhemingly likely circumstances it is my opinion that the OP should go for readability rather than try to save 3 nanoseconds from a call which is going to take at least a second over the net.
If you (or anyone else) believes that your solution is more readable and more maintainable, then of course you should follow your instincts. If that is the case, then I consider you a truely exceptional person, and I hope you have the sort of luck which will get you nothing but deeply-nested, unstructured, monolithic blocks of code for the rest of your life. Someone has to do it, and I'd rather it was you than me 
Ian
On 23/05/07, stas <stas.t refi... gmail.com> wrote:
denis, ian
just think about other systems than big commercial software. please! i know you have a good imagination. consider this: a php script receives a giant text file (say 2 gb) with a csv data which needs to be parsed and only load into a database the lines where the column x has one of constant values. thanks denis we know that the switch statement will take 3 to 7 times faster than in_array () and much more faster than a function call. the question now: why should we care about best practices of enterprise application development for the script where the speed (efficiency) is crucial? why should we follow these j2ee guidelines here? you say because it's more readable. i say ask your user waiting for the file to be processed.
-- free web developer tools (now with sitemap support):
- Show quoted text -
Hi Fellas, Thanks a lot for the replies on this. You seem to have started an interesting debate! I'm not sure if it really matters at this point but for the record, I am developing a web application but it is not importing any 2GB text files!! I was simply looking for an easier/ quicker way to write a conditional like the one above as I was finding myself typing long messy statements like that quite often. I was in fact, hoping there was a function in PHP that emulates the WHERE x IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently not! I guess inarray() is the closest thing, however if switch is faster, perhaps it would be feasible to write a universal function that crosschecks a group of passed vars using switch? Would something like that be noticeably slower than the standard method in my original post? I'm not overly concerned with speed, I mean it will only be a max of 10 checks at a time anyway. Im not even sure how to send an indefinate number of paramaters to a function?
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP -~----------~----~----~----~------~----~------~--~---
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-23 10:13:26 |
P.P.S. I _am_ young, yet that does not matter at all. Let us
stop this
flamebait, ok? =^o^=
On 5/23/07, Denis <sokolov.denis gmail.com> wrote:
> Where have you seen my nick? ^_^
>
> The fact that you get insulted over a friendly reminder
of
> func_get_arg() tells something about you. ^_~ You give
totally lame
> reasoning why not to use it. "don't use unneded
> wrappers."
> In response to this:
> "hoping there was a function in PHP that emulates
the WHERE x
> IN(1,3,4,5,6,7,9,etc) syntax in mySql"
> You offered to use
> to pass unlimited number of parameters into your
function use hash
> array like this:
> -----code-------
> func (array ('var1'=>'this is a value for var1'
> , 'var2'=>'this is a value for var2'
> ));
> ?>
> -----/code-------
> Effectively offering the following construction: in($x,
array(1,2,3)),
> what's the same as in_array(). I offered alternative to
simplify this:
> -----code-------
> in($x, 1,2,3);
> -----/code-------
> because we were talking about readability and
simplicity, not speed.
> And here you step in saying "func_get_arg" is
too complicated. Oh,
> come on, don't be shy. Noone can remember all 5312
functions of PHP.
> ^_~
>
> Don't worry, just be happy!
>
>
> P.S. My simple benchmarks show what they need to show -
what's faster,
> not precise numbers :P
>
> On 5/23/07, Stas Trefilov <stas.trefilov gmail.com> wrote:
> > denis, you make common mistakes which disclose a
very young and
> > unexperienced person behind your nick. don't judge
other's ability in
> > programming languages by the fact that the person
does not use
> > func_get_args. don't substitute a simple function
call to a core function by
> > 3 core function calls and one user function call.
don't use unneded
> > wrappers.
> >
> > good luck on your hard way.
> >
> >
> > -stas
> >
> >
> >
> >
> > Le 23 mai 07 � 13:15, Denis a �crit :
> >
> >
> >
> > Someone (*poke*, Stas) does not know PHP properly
^_^
> >
> > function in()
> > {
> > $list = func_get_args();
> > $item = array_shift($list);
> > return in_array($item, $list);
> > }
> >
> > if (in($number,1,3,5,17)) do_foo();
> >
> > ^_~
> >
> >
> > On 5/23/07, stas <stas.trefilov gmail.com> wrote:
> >
> > next time be more precise in your questions if you
don't want to be a
> > cause of holy wars
> >
> > in fact switch is only faster if you have a
constant number of values.
> > if your values vary then put them in an array and
use in_array
> > ($vars). don't forget to set some meaningful name
for your array. try
> > not to write functions as simple wrappers around
core php functions,
> > reuse them!
> >
> > to pass unlimited number of parameters into your
function use hash
> > array like this:
> >
> > <?php
> > function func ($params)
> > {
> > $var = $param['var1'];
> > echo $var1;
> > }
> >
> > func (array ('var1'=>'this is a value for var1'
> > , 'var2'=>'this is a value for var2'
> > ));
> > ?>
> >
> > --
> > free web developer tools (now with sitemap
support):
> > http://osbtools.goog
lepages.com/
> >
> >
> > On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> > On May 23, 10:18 am, "Stas Trefilov"
<stas.trefi... gmail.com> wrote:
> >
> >
> >
> >
> > once again, ian, no one was talking here about
readability or
> > maintainability. best practices may be good to
know for classical
> > situations but the question here was about
effectiveness -- something
> > that is not on mind of most classical developers.
> >
> >
> > and yes, this is php forum. we don't count
chances, we solve real problems.
> >
> >
> > On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
> >
> >
> >
> > Stas,
> >
> >
> >
> > No-one is saying that it isn't possible that in
extreme conditions there may
> > be an argument for speed over readability. In a
previous post I said "Unless
> > you are in a tight loop, the array solution will
have no discernable effect
> > on speed". I would consider loading a 2gb csv
file into a database a 'tight
> > loop'.
> >
> >
> >
> > If loading 20 million records or thereabouts is a
regular feature of a
> > system and users are being forced to wait while
they are loaded, then there
> > is something seriously wrong with the design of
the system.
> >
> >
> >
> > I am trying to talk about general programming good
practice, and of course
> > it will be possible to come up with extreme
examples where general
> > principles should be abandonded in the interests
of some other
> > consideration. There are times when the fastest
data transfer method is to
> > put 10 boxes of DVDs in a taxi, but it is not the
best way to transfer data
> > under most conditions.
> >
> >
> >
> > This is a PHP forum. The chances are that the OP
is developing a web
> > application. The chances are also in favour of the
OP's program is not
> > attempting to load 20m records from a csv file
into a database every time
> > any user anywhere in the world hits Enter. The
chances are that the OP just
> > wants to check a condition.
> >
> >
> >
> > In these overwhemingly likely circumstances it is
my opinion that the OP
> > should go for readability rather than try to save
3 nanoseconds from a call
> > which is going to take at least a second over the
net.
> >
> >
> >
> > If you (or anyone else) believes that your
solution is more readable and
> > more maintainable, then of course you should
follow your instincts. If that
> > is the case, then I consider you a truely
exceptional person, and I hope you
> > have the sort of luck which will get you nothing
but deeply-nested,
> > unstructured, monolithic blocks of code for the
rest of your life. Someone
> > has to do it, and I'd rather it was you than me
> >
> >
> >
> > Ian
> >
> >
> >
> > On 23/05/07, stas <stas.trefi... gmail.com> wrote:
> >
> >
> >
> > denis, ian
> >
> >
> >
> > just think about other systems than big commercial
software. please! i
> > know you have a good imagination. consider this: a
php script receives
> > a giant text file (say 2 gb) with a csv data which
needs to be parsed
> > and only load into a database the lines where the
column x has one of
> > constant values. thanks denis we know that the
switch statement will
> > take 3 to 7 times faster than in_array () and much
more faster than a
> > function call. the question now: why should we
care about best
> > practices of enterprise application development
for the script where
> > the speed (efficiency) is crucial? why should we
follow these j2ee
> > guidelines here? you say because it's more
readable. i say ask your
> > user waiting for the file to be processed.
> >
> >
> >
> > --
> > free web developer tools (now with sitemap
support):
> > http://osbtools
.googlepages.com/-Hide quoted text -
> >
> >
> > - Show quoted text -
> >
> > Hi Fellas,
> > Thanks a lot for the replies on this. You seem to
have started an
> > interesting debate! I'm not sure if it really
matters at this point
> > but for the record, I am developing a web
application but it is not
> > importing any 2GB text files!! I was simply
looking for an easier/
> > quicker way to write a conditional like the one
above as I was finding
> > myself typing long messy statements like that
quite often. I was in
> > fact, hoping there was a function in PHP that
emulates the WHERE x
> > IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently
not! I guess
> > inarray() is the closest thing, however if switch
is faster, perhaps
> > it would be feasible to write a universal function
that crosschecks a
> > group of passed vars using switch? Would something
like that be
> > noticeably slower than the standard method in my
original post? I'm
> > not overly concerned with speed, I mean it will
only be a max of 10
> > checks at a time anyway. Im not even sure how to
send an indefinate
> > number of paramaters to a function?
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > > >
> >
> >
> >
>
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-23 10:12:52 |
Where have you seen my nick? ^_^
The fact that you get insulted over a friendly reminder of
func_get_arg() tells something about you. ^_~ You give
totally lame
reasoning why not to use it. "don't use unneded
wrappers."
In response to this:
"hoping there was a function in PHP that emulates the
WHERE x
IN(1,3,4,5,6,7,9,etc) syntax in mySql"
You offered to use
to pass unlimited number of parameters into your function
use hash
array like this:
-----code-------
func (array ('var1'=>'this is a value for var1'
, 'var2'=>'this is a value for var2'
));
?>
-----/code-------
Effectively offering the following construction: in($x,
array(1,2,3)),
what's the same as in_array(). I offered alternative to
simplify this:
-----code-------
in($x, 1,2,3);
-----/code-------
because we were talking about readability and simplicity,
not speed.
And here you step in saying "func_get_arg" is too
complicated. Oh,
come on, don't be shy. Noone can remember all 5312 functions
of PHP.
^_~
Don't worry, just be happy!
P.S. My simple benchmarks show what they need to show -
what's faster,
not precise numbers :P
On 5/23/07, Stas Trefilov <stas.trefilov gmail.com> wrote:
> denis, you make common mistakes which disclose a very
young and
> unexperienced person behind your nick. don't judge
other's ability in
> programming languages by the fact that the person does
not use
> func_get_args. don't substitute a simple function call
to a core function by
> 3 core function calls and one user function call. don't
use unneded
> wrappers.
>
> good luck on your hard way.
>
>
> -stas
>
>
>
>
> Le 23 mai 07 � 13:15, Denis a �crit :
>
>
>
> Someone (*poke*, Stas) does not know PHP properly ^_^
>
> function in()
> {
> $list = func_get_args();
> $item = array_shift($list);
> return in_array($item, $list);
> }
>
> if (in($number,1,3,5,17)) do_foo();
>
> ^_~
>
>
> On 5/23/07, stas <stas.trefilov gmail.com> wrote:
>
> next time be more precise in your questions if you
don't want to be a
> cause of holy wars
>
> in fact switch is only faster if you have a constant
number of values.
> if your values vary then put them in an array and use
in_array
> ($vars). don't forget to set some meaningful name for
your array. try
> not to write functions as simple wrappers around core
php functions,
> reuse them!
>
> to pass unlimited number of parameters into your
function use hash
> array like this:
>
> <?php
> function func ($params)
> {
> $var = $param['var1'];
> echo $var1;
> }
>
> func (array ('var1'=>'this is a value for var1'
> , 'var2'=>'this is a value for var2'
> ));
> ?>
>
> --
> free web developer tools (now with sitemap support):
> http://osbtools.goog
lepages.com/
>
>
> On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> On May 23, 10:18 am, "Stas Trefilov"
<stas.trefi... gmail.com> wrote:
>
>
>
>
> once again, ian, no one was talking here about
readability or
> maintainability. best practices may be good to know for
classical
> situations but the question here was about
effectiveness -- something
> that is not on mind of most classical developers.
>
>
> and yes, this is php forum. we don't count chances, we
solve real problems.
>
>
> On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
>
>
>
> Stas,
>
>
>
> No-one is saying that it isn't possible that in extreme
conditions there may
> be an argument for speed over readability. In a
previous post I said "Unless
> you are in a tight loop, the array solution will have
no discernable effect
> on speed". I would consider loading a 2gb csv file
into a database a 'tight
> loop'.
>
>
>
> If loading 20 million records or thereabouts is a
regular feature of a
> system and users are being forced to wait while they
are loaded, then there
> is something seriously wrong with the design of the
system.
>
>
>
> I am trying to talk about general programming good
practice, and of course
> it will be possible to come up with extreme examples
where general
> principles should be abandonded in the interests of
some other
> consideration. There are times when the fastest data
transfer method is to
> put 10 boxes of DVDs in a taxi, but it is not the best
way to transfer data
> under most conditions.
>
>
>
> This is a PHP forum. The chances are that the OP is
developing a web
> application. The chances are also in favour of the OP's
program is not
> attempting to load 20m records from a csv file into a
database every time
> any user anywhere in the world hits Enter. The chances
are that the OP just
> wants to check a condition.
>
>
>
> In these overwhemingly likely circumstances it is my
opinion that the OP
> should go for readability rather than try to save 3
nanoseconds from a call
> which is going to take at least a second over the net.
>
>
>
> If you (or anyone else) believes that your solution is
more readable and
> more maintainable, then of course you should follow
your instincts. If that
> is the case, then I consider you a truely exceptional
person, and I hope you
> have the sort of luck which will get you nothing but
deeply-nested,
> unstructured, monolithic blocks of code for the rest of
your life. Someone
> has to do it, and I'd rather it was you than me
>
>
>
> Ian
>
>
>
> On 23/05/07, stas <stas.trefi... gmail.com> wrote:
>
>
>
> denis, ian
>
>
>
> just think about other systems than big commercial
software. please! i
> know you have a good imagination. consider this: a php
script receives
> a giant text file (say 2 gb) with a csv data which
needs to be parsed
> and only load into a database the lines where the
column x has one of
> constant values. thanks denis we know that the switch
statement will
> take 3 to 7 times faster than in_array () and much more
faster than a
> function call. the question now: why should we care
about best
> practices of enterprise application development for the
script where
> the speed (efficiency) is crucial? why should we follow
these j2ee
> guidelines here? you say because it's more readable. i
say ask your
> user waiting for the file to be processed.
>
>
>
> --
> free web developer tools (now with sitemap support):
> http://osbtools
.googlepages.com/-Hide quoted text -
>
>
> - Show quoted text -
>
> Hi Fellas,
> Thanks a lot for the replies on this. You seem to have
started an
> interesting debate! I'm not sure if it really matters
at this point
> but for the record, I am developing a web application
but it is not
> importing any 2GB text files!! I was simply looking for
an easier/
> quicker way to write a conditional like the one above
as I was finding
> myself typing long messy statements like that quite
often. I was in
> fact, hoping there was a function in PHP that emulates
the WHERE x
> IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently not! I
guess
> inarray() is the closest thing, however if switch is
faster, perhaps
> it would be feasible to write a universal function that
crosschecks a
> group of passed vars using switch? Would something like
that be
> noticeably slower than the standard method in my
original post? I'm
> not overly concerned with speed, I mean it will only be
a max of 10
> checks at a time anyway. Im not even sure how to send
an indefinate
> number of paramaters to a function?
>
>
>
>
>
>
>
>
>
>
>
>
> >
>
>
>
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-23 13:18:15 |
denis
read well the postings. i proposed using array to pass the
"indefinate
number of paramaters to a function", not in response to
emulate the
sql _in_ syntax.
i never said "func_get_arg is too complicated".
stop falsifying your benchmarks. you didn't even try to run
them
before exposing false results to the public. if you would
you could
find out that define cannot deal with arrays. seems like you
are the
last one to find it out.
my own benchmarks indicate that for the proposed 4 solutions
(using
if, switch, in_array and wrapper function that calls itself
an
in_array) the results vary depending on the following
conditions:
whether the search value was at the beginning of the list,
at the end
of the list or not found in the list. here are the results:
1/ first in the list:
if = 0,8
switch = 1,3
in_array = 5,1
wrapper = 5,5
2/ last in the list:
if = 1,5
switch = 1,9
in_array = 5,1
wrapper = 5,8
3/ not in the list:
if = 1,4
switch = 1,8
in_array = 5,1
wrapper = 5,8
the benchmark file is attached. you may use it next time to
produce
real benchmarks
--
free web developer tools (now with sitemap support):
http://osbtools.goog
lepages.com/
On 5/23/07, Denis <sokolov.denis gmail.com> wrote:
> Where have you seen my nick? ^_^
>
> The fact that you get insulted over a friendly reminder
of
> func_get_arg() tells something about you. ^_~ You give
totally lame
> reasoning why not to use it. "don't use unneded
> wrappers."
> In response to this:
> "hoping there was a function in PHP that emulates
the WHERE x
> IN(1,3,4,5,6,7,9,etc) syntax in mySql"
> You offered to use
> to pass unlimited number of parameters into your
function use hash
> array like this:
> -----code-------
> func (array ('var1'=>'this is a value for var1'
> , 'var2'=>'this is a value for var2'
> ));
> ?>
> -----/code-------
> Effectively offering the following construction: in($x,
array(1,2,3)),
> what's the same as in_array(). I offered alternative to
simplify this:
> -----code-------
> in($x, 1,2,3);
> -----/code-------
> because we were talking about readability and
simplicity, not speed.
> And here you step in saying "func_get_arg" is
too complicated. Oh,
> come on, don't be shy. Noone can remember all 5312
functions of PHP.
> ^_~
>
> Don't worry, just be happy!
>
>
> P.S. My simple benchmarks show what they need to show -
what's faster,
> not precise numbers :P
>
> On 5/23/07, Stas Trefilov <stas.trefilov gmail.com> wrote:
> > denis, you make common mistakes which disclose a
very young and
> > unexperienced person behind your nick. don't judge
other's ability in
> > programming languages by the fact that the person
does not use
> > func_get_args. don't substitute a simple function
call to a core function by
> > 3 core function calls and one user function call.
don't use unneded
> > wrappers.
> >
> > good luck on your hard way.
> >
> >
> > -stas
> >
> >
> >
> >
> > Le 23 mai 07 � 13:15, Denis a �crit :
> >
> >
> >
> > Someone (*poke*, Stas) does not know PHP properly
^_^
> >
> > function in()
> > {
> > $list = func_get_args();
> > $item = array_shift($list);
> > return in_array($item, $list);
> > }
> >
> > if (in($number,1,3,5,17)) do_foo();
> >
> > ^_~
> >
> >
> > On 5/23/07, stas <stas.trefilov gmail.com> wrote:
> >
> > next time be more precise in your questions if you
don't want to be a
> > cause of holy wars
> >
> > in fact switch is only faster if you have a
constant number of values.
> > if your values vary then put them in an array and
use in_array
> > ($vars). don't forget to set some meaningful name
for your array. try
> > not to write functions as simple wrappers around
core php functions,
> > reuse them!
> >
> > to pass unlimited number of parameters into your
function use hash
> > array like this:
> >
> > <?php
> > function func ($params)
> > {
> > $var = $param['var1'];
> > echo $var1;
> > }
> >
> > func (array ('var1'=>'this is a value for var1'
> > , 'var2'=>'this is a value for var2'
> > ));
> > ?>
> >
> > --
> > free web developer tools (now with sitemap
support):
> > http://osbtools.goog
lepages.com/
> >
> >
> > On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> > On May 23, 10:18 am, "Stas Trefilov"
<stas.trefi... gmail.com> wrote:
> >
> >
> >
> >
> > once again, ian, no one was talking here about
readability or
> > maintainability. best practices may be good to
know for classical
> > situations but the question here was about
effectiveness -- something
> > that is not on mind of most classical developers.
> >
> >
> > and yes, this is php forum. we don't count
chances, we solve real problems.
> >
> >
> > On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
> >
> >
> >
> > Stas,
> >
> >
> >
> > No-one is saying that it isn't possible that in
extreme conditions there may
> > be an argument for speed over readability. In a
previous post I said "Unless
> > you are in a tight loop, the array solution will
have no discernable effect
> > on speed". I would consider loading a 2gb csv
file into a database a 'tight
> > loop'.
> >
> >
> >
> > If loading 20 million records or thereabouts is a
regular feature of a
> > system and users are being forced to wait while
they are loaded, then there
> > is something seriously wrong with the design of
the system.
> >
> >
> >
> > I am trying to talk about general programming good
practice, and of course
> > it will be possible to come up with extreme
examples where general
> > principles should be abandonded in the interests
of some other
> > consideration. There are times when the fastest
data transfer method is to
> > put 10 boxes of DVDs in a taxi, but it is not the
best way to transfer data
> > under most conditions.
> >
> >
> >
> > This is a PHP forum. The chances are that the OP
is developing a web
> > application. The chances are also in favour of the
OP's program is not
> > attempting to load 20m records from a csv file
into a database every time
> > any user anywhere in the world hits Enter. The
chances are that the OP just
> > wants to check a condition.
> >
> >
> >
> > In these overwhemingly likely circumstances it is
my opinion that the OP
> > should go for readability rather than try to save
3 nanoseconds from a call
> > which is going to take at least a second over the
net.
> >
> >
> >
> > If you (or anyone else) believes that your
solution is more readable and
> > more maintainable, then of course you should
follow your instincts. If that
> > is the case, then I consider you a truely
exceptional person, and I hope you
> > have the sort of luck which will get you nothing
but deeply-nested,
> > unstructured, monolithic blocks of code for the
rest of your life. Someone
> > has to do it, and I'd rather it was you than me
> >
> >
> >
> > Ian
> >
> >
> >
> > On 23/05/07, stas <stas.trefi... gmail.com> wrote:
> >
> >
> >
> > denis, ian
> >
> >
> >
> > just think about other systems than big commercial
software. please! i
> > know you have a good imagination. consider this: a
php script receives
> > a giant text file (say 2 gb) with a csv data which
needs to be parsed
> > and only load into a database the lines where the
column x has one of
> > constant values. thanks denis we know that the
switch statement will
> > take 3 to 7 times faster than in_array () and much
more faster than a
> > function call. the question now: why should we
care about best
> > practices of enterprise application development
for the script where
> > the speed (efficiency) is crucial? why should we
follow these j2ee
> > guidelines here? you say because it's more
readable. i say ask your
> > user waiting for the file to be processed.
> >
> >
> >
> > --
> > free web developer tools (now with sitemap
support):
> > http://osbtools
.googlepages.com/-Hide quoted text -
> >
> >
> > - Show quoted text -
> >
> > Hi Fellas,
> > Thanks a lot for the replies on this. You seem to
have started an
> > interesting debate! I'm not sure if it really
matters at this point
> > but for the record, I am developing a web
application but it is not
> > importing any 2GB text files!! I was simply
looking for an easier/
> > quicker way to write a conditional like the one
above as I was finding
> > myself typing long messy statements like that
quite often. I was in
> > fact, hoping there was a function in PHP that
emulates the WHERE x
> > IN(1,3,4,5,6,7,9,etc) syntax in mySql. Evidently
not! I guess
> > inarray() is the closest thing, however if switch
is faster, perhaps
> > it would be feasible to write a universal function
that crosschecks a
> > group of passed vars using switch? Would something
like that be
> > noticeably slower than the standard method in my
original post? I'm
> > not overly concerned with speed, I mean it will
only be a max of 10
> > checks at a time anyway. Im not even sure how to
send an indefinate
> > number of paramaters to a function?
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > >
> >
> >
> >
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
|
| Re: Simplifying a basic if statement
conditional |

|
2007-05-24 03:50:11 |
Stas, you are making strong accusations, which could be
quite
insultive. I added define for clarity after tests and I
always, always
fall for this error. It's counterintuitive, why can't array
be
define()d.
If you're interested, my file attached and stop this
flamebait once in a while.
By the way, could you please explain to me, why need to make
that
"warmup" in test of yours? Why to include it in
first timings? Of
course, it does not take too much time, but it looks like it
would
still add a bit.
On 5/23/07, Stas Trefilov <stas.trefilov gmail.com> wrote:
> stop falsifying your benchmarks. you didn't even try to
run them
> before exposing false results to the public. if you
would you could
> find out that define cannot deal with arrays. seems
like you are the
> last one to find it out.
>
> the benchmark file is attached. you may use it next
time to produce
> real benchmarks
>
> --
> free web developer tools (now with sitemap support):
> http://osbtools.goog
lepages.com/
>
>
> On 5/23/07, Denis <sokolov.denis gmail.com> wrote:
> > Where have you seen my nick? ^_^
> >
> > The fact that you get insulted over a friendly
reminder of
> > func_get_arg() tells something about you. ^_~ You
give totally lame
> > reasoning why not to use it. "don't use
unneded
> > wrappers."
> > In response to this:
> > "hoping there was a function in PHP that
emulates the WHERE x
> > IN(1,3,4,5,6,7,9,etc) syntax in mySql"
> > You offered to use
> > to pass unlimited number of parameters into your
function use hash
> > array like this:
> > -----code-------
> > func (array ('var1'=>'this is a value for var1'
> > , 'var2'=>'this is a value for var2'
> > ));
> > ?>
> > -----/code-------
> > Effectively offering the following construction:
in($x, array(1,2,3)),
> > what's the same as in_array(). I offered
alternative to simplify this:
> > -----code-------
> > in($x, 1,2,3);
> > -----/code-------
> > because we were talking about readability and
simplicity, not speed.
> > And here you step in saying
"func_get_arg" is too complicated. Oh,
> > come on, don't be shy. Noone can remember all 5312
functions of PHP.
> > ^_~
> >
> > Don't worry, just be happy!
> >
> >
> > P.S. My simple benchmarks show what they need to
show - what's faster,
> > not precise numbers :P
> >
> > On 5/23/07, Stas Trefilov <stas.trefilov gmail.com> wrote:
> > > denis, you make common mistakes which
disclose a very young and
> > > unexperienced person behind your nick. don't
judge other's ability in
> > > programming languages by the fact that the
person does not use
> > > func_get_args. don't substitute a simple
function call to a core function by
> > > 3 core function calls and one user function
call. don't use unneded
> > > wrappers.
> > >
> > > good luck on your hard way.
> > >
> > >
> > > -stas
> > >
> > >
> > >
> > >
> > > Le 23 mai 07 � 13:15, Denis a �crit :
> > >
> > >
> > >
> > > Someone (*poke*, Stas) does not know PHP
properly ^_^
> > >
> > > function in()
> > > {
> > > $list = func_get_args();
> > > $item = array_shift($list);
> > > return in_array($item, $list);
> > > }
> > >
> > > if (in($number,1,3,5,17)) do_foo();
> > >
> > > ^_~
> > >
> > >
> > > On 5/23/07, stas <stas.trefilov gmail.com> wrote:
> > >
> > > next time be more precise in your questions
if you don't want to be a
> > > cause of holy wars
> > >
> > > in fact switch is only faster if you have a
constant number of values.
> > > if your values vary then put them in an array
and use in_array
> > > ($vars). don't forget to set some meaningful
name for your array. try
> > > not to write functions as simple wrappers
around core php functions,
> > > reuse them!
> > >
> > > to pass unlimited number of parameters into
your function use hash
> > > array like this:
> > >
> > > <?php
> > > function func ($params)
> > > {
> > > $var = $param['var1'];
> > > echo $var1;
> > > }
> > >
> > > func (array ('var1'=>'this is a value for
var1'
> > > , 'var2'=>'this is a value for var2'
> > > ));
> > > ?>
> > >
> > > --
> > > free web developer tools (now with sitemap
support):
> > > http://osbtools.goog
lepages.com/
> > >
> > >
> > > On May 23, 12:18 pm, Ciaran <cronok... hotmail.com> wrote:
> > > On May 23, 10:18 am, "Stas
Trefilov" <stas.trefi... gmail.com> wrote:
> > >
> > >
> > >
> > >
> > > once again, ian, no one was talking here
about readability or
> > > maintainability. best practices may be good
to know for classical
> > > situations but the question here was about
effectiveness -- something
> > > that is not on mind of most classical
developers.
> > >
> > >
> > > and yes, this is php forum. we don't count
chances, we solve real problems.
> > >
> > >
> > > On 5/23/07, Ian Bambury <ianbamb... gmail.com> wrote:
> > >
> > >
> > >
> > > Stas,
> > >
> > >
> > >
> > > No-one is saying that it isn't possible that
in extreme conditions there may
> > > be an argument for speed over readability. In
a previous post I said "Unless
> > > you are in a tight loop, the array solution
will have no discernable effect
> > > on speed". I would consider loading a
2gb csv file into a database a 'tight
> > > loop'.
> > >
> > >
> > >
> > > If loading 20 million records or thereabouts
is a regular feature of a
> > > system and users are being forced to wait
while they are loaded, then there
> > > is something seriously wrong with the design
of the system.
> > >
> > >
> > >
> > > I am trying to talk about general programming
good practice, and of course
> > > it will be possible to come up with extreme
examples where general
> > > principles should be abandonded in the
interests of some other
> > > consideration. There are times when the
fastest data transfer method is to
> > > put 10 boxes of DVDs in a taxi, but it is not
the best way to transfer data
> > > under most conditions.
> > >
> > >
> > >
> > > This is a PHP forum. The chances are that the
OP is developing a web
> > > application. The chances are also in favour
of the OP's program is not
> > > attempting to load 20m records from a csv
file into a database every time
> > > any user anywhere in the world hits Enter.
The chances are that the OP just
> > > wants to check a condition.
> > >
> > >
> > >
> > > In these overwhemingly likely circumstances
it is my opinion that the OP
> > > should go for readability rather than try to
save 3 nanoseconds from a call
> > > which is going to take at least a second over
the net.
> > >
> > >
> > >
> > > If you (or anyone else) believes that your
solution is more readable and
> > > more maintainable, then of course you should
follow your instincts. If that
> > > is the case, then I consider you a truely
exceptional person, and I hope you
> > > have the sort of luck which will get you
nothing but deeply-nested,
> > > unstructured, monolithic blocks of code for
the rest of your life. Someone
> > > has to do it, and I'd rather it was you than
me
> > >
> > >
> > >
> > > Ian
> > >
> > >
> > >
> > > On 23/05/07, stas <stas.trefi... gmail.com> wrote:
> > >
> > >
> > >
> > > denis, ian
> > >
> > >
> > >
> > > just think about other systems than big
commercial software. please! i
> > > know you have a good imagination. consider
this: a php script receives
> > > a giant text file (say 2 gb) with a csv data
which needs to be parsed
> > > and only load into a database the lines where
the column x has one of
> > > constant values. thanks denis we know that
the switch statement will
> > > take 3 to 7 times faster than in_array () and
much more faster than a
> > > function call. the question now: why should
we care about best
> > > practices of enterprise application
development for the script where
> > > the speed (efficiency) is crucial? why should
we follow these j2ee
> > > guidelines here? you say because it's more
readable. i say ask your
> > > user waiting for the file to be processed.
> > >
> > >
> > >
> > > --
> > > free web developer tools (now with sitemap
support):
> > > http://osbtools
.googlepages.com/-Hide quoted text -
> > >
> > >
> > > - Show quoted text -
> > >
> > > Hi Fellas,
> > > Thanks a lot for the replies on this. You
seem to have started an
> > > interesting debate! I'm not sure if it really
matters at this point
> > > but for the record, I am developing a web
application but it is not
> > > importing any 2GB text files!! I was simply
looking for an easier/
> > > quicker way to write a conditional like the
one above as I was finding
> > > myself typing long messy statements like that
quite often. I was in
> > > fact, hoping there was a function in PHP that
emulates the WHERE x
> > > IN(1,3,4,5,6,7,9,etc) syntax in mySql.
Evidently not! I guess
> > > inarray() is the closest thing, however if
switch is faster, perhaps
> > > it would be feasible to write a universal
function that crosschecks a
> > > group of passed vars using switch? Would
something like that be
> > > noticeably slower than the standard method in
my original post? I'm
> > > not overly concerned with speed, I mean it
will only be a max of 10
> > > checks at a time anyway. Im not even sure how
to send an indefinate
> > > number of paramaters to a function?
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > >
> > >
> > >
> > >
> >
> > >
> >
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.
For information or project assistance please visit :
http://www.360psg.com
You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHP googlegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---
|
|
|
|
|