469,593 Members | 1,984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

Comparing two functions. Need review...

All,
I am comparing to functions to see which is "better". In better, I mean more
efficient, optimize, faster, etc.
I have read other posts from other boards, but I'm not really sure of the
benefits or if there is a speed increase
with one vs. the other.

FIRST VERSION
function is_active($module) {
global $prefix, $dbi;
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
list ($act) = sql_fetch_row($result, $dbi);
if (!$result OR $act == 0) {
return 0;
} else {
return 1;
}
}

SECOND VERSION
function is_active($module) {
global $dbi;
static $save;
if (is_array($save)) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
while ($act = sql_fetch_row($result, $dbi)) {
$save[$act[0]] = 1;
}
if (isset($save[$module])) return ($save[$module]);
return 0;
}
Jul 17 '05 #1
11 1742
This exact same question was posted on 4/25. I'm not sure why you're
re-posting it... if you really want to find out, shove it in a for loop, run
it 10,000 times, and time it.

Dimension7 wrote:
All,
I am comparing to functions to see which is "better". In better, I
mean more efficient, optimize, faster, etc.
I have read other posts from other boards, but I'm not really sure of
the benefits or if there is a speed increase
with one vs. the other.

FIRST VERSION
function is_active($module) {
global $prefix, $dbi;
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
list ($act) = sql_fetch_row($result, $dbi);
if (!$result OR $act == 0) {
return 0;
} else {
return 1;
}
}

SECOND VERSION
function is_active($module) {
global $dbi;
static $save;
if (is_array($save)) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
while ($act = sql_fetch_row($result, $dbi)) {
$save[$act[0]] = 1;
}
if (isset($save[$module])) return ($save[$module]);
return 0;
}

Jul 17 '05 #2
Hi ,

On Thu, 29 Apr 2004 20:09:41 -0800, "Dimension7" <di*******@seven.com>
wrote:
All,
I am comparing to functions to see which is "better". In better, I mean more
efficient, optimize, faster, etc.
I have read other posts from other boards, but I'm not really sure of the
benefits or if there is a speed increase
with one vs. the other.
FIRST VERSION
(Wrapped database query)>SECOND VERSION

Wrapped and cached database query

It depends how you use it. If you will call it at least twice with the
same module, it will probably be worth caching the database results.
This also depends on how long your pages run at all and how much
memory is clogged up by that, which cannot be used by other pages at
the same time.

The second version gives you also the freedom to "reuse" the query,
without having to worry to much of a lag, because of many unnecessary
queries. This way, it decouples your functions/ modules.

If your site is frequented by a moderate number of users only (ie.
selctive access rights, small community) the second version migt be
better. I have a generalised version for lookup tables in the Toolkit
in the signature (gadm_intertablecommand.php)

HTH, Jochen


--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #3
On Fri, 30 Apr 2004 00:20:23 -0400
"Agelmar" <if**********@comcast.net> wrote:
This exact same question was posted on 4/25. I'm not sure why you're
re-posting it... if you really want to find out, shove it in a for
loop, run it 10,000 times, and time it.
What? Is that how you do? And why top-post? Maybe you should read
about how to answer messages on USENET and then study some algorithm
analysis basics.
Dimension7 wrote:
All,
I am comparing to functions to see which is "better". In better, I
mean more efficient, optimize, faster, etc.
I have read other posts from other boards, but I'm not really sure
of the benefits or if there is a speed increase
with one vs. the other.
Your functions have the same performance.
FIRST VERSION
function is_active($module) {
global $prefix, $dbi;
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
list ($act) = sql_fetch_row($result, $dbi);
Why are you using list() ? sql_fetch_row() doesn't exist.
if (!$result OR $act == 0) {
return 0;
} else {
return 1;
}
}

SECOND VERSION
function is_active($module) {
global $dbi;
static $save;
if (is_array($save)) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
while ($act = sql_fetch_row($result, $dbi)) {
$save[$act[0]] = 1;
}
Why set 1 to $save[$act[0]] more than once? If while() will run only
once, why use while() then?
if (isset($save[$module])) return ($save[$module]);
return 0;
}

Jul 17 '05 #4
Kelly Thompson wrote:
On Fri, 30 Apr 2004 00:20:23 -0400
"Agelmar" <if**********@comcast.net> wrote:
This exact same question was posted on 4/25. I'm not sure why you're
re-posting it... if you really want to find out, shove it in a for
loop, run it 10,000 times, and time it.


What? Is that how you do? And why top-post? Maybe you should read
about how to answer messages on USENET and then study some algorithm
analysis basics.


1. "Is that how you do?" Perhaps you could benefit from a remedial English
course (or at least a remedial programming course, so you can figure out
what his function is really doing.)
2. I top-posted because I felt like it. Before you chide me on the rules of
Usenet, you should know that I'm the one who created this group.
(http://www.php.net/news-2002.php scroll down to "New comp.lang.php
newsgroup started")
3. I've studied algorithm complexity analysis [I'm at Michigan Engineering],
I think that the problem lies in the fact that you have a fundamental
misunderstanding of what his function does. See below.
Dimension7 wrote:
All,
I am comparing to functions to see which is "better". In better, I
mean more efficient, optimize, faster, etc.
I have read other posts from other boards, but I'm not really sure
of the benefits or if there is a speed increase
with one vs. the other.
Your functions have the same performance.


False
FIRST VERSION
function is_active($module) {
global $prefix, $dbi;
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
list ($act) = sql_fetch_row($result, $dbi);
Why are you using list() ? sql_fetch_row() doesn't exist.


He's using list because *sql_fetch_row returns an array, but he doesn't want
an array with just one value, so he's using list to store that one value in
a regular singular variable. Simple enough.
if (!$result OR $act == 0) {
return 0;
} else {
return 1;
}
}

SECOND VERSION
function is_active($module) {
global $dbi;
static $save;
if (is_array($save)) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
while ($act = sql_fetch_row($result, $dbi)) {
$save[$act[0]] = 1;
}
Why set 1 to $save[$act[0]] more than once? If while() will run only
once, why use while() then?


while() will run more than once, it will run as many times as there are
results returned by the query. As for setting "$save[$act[0]] more than
once" you should realize that $save is an array, and each time through the
while loop $act[0] is going to have a different value, hence he's populating
the $save array with a 1 in all active modules. As for why this version may
be more efficient, he's using static variables, and so if the variable is
set (called the query twice while the module is still active) the query will
not be performed a second time, rather the result from the static variable
will be returned. OTOH there will be some additional overhead, and the 2nd
may be worse if the traffic is low and therefore the static variable is
usually unset at query time.

if (isset($save[$module])) return ($save[$module]);
return 0;
}


// Ian Fette
// Proponent, comp.lang.php
Jul 17 '05 #5
Agelmar, excellent reply, very thorough.
Thanks for taking time to post =)

Jochen Daum, also, thank you. Very informative post,
nice link BTW.
Jul 17 '05 #6
On Fri, 30 Apr 2004 08:38:03 -0400 "Agelmar"
<if**********@comcast.net> wrote:
Kelly Thompson wrote:
On Fri, 30 Apr 2004 00:20:23 -0400
"Agelmar" <if**********@comcast.net> wrote:
This exact same question was posted on 4/25. I'm not sure whyyou're> re-posting it... if you really want to find out, shove it
in a for> loop, run it 10,000 times, and time it.

What? Is that how you do? And why top-post? Maybe you should read
about how to answer messages on USENET and then study some
algorithm analysis basics.
[snip]
2. I top-posted because I felt like it. Before you chide me on the
rules of Usenet, you should know that I'm the one who created this
group.(http://www.php.net/news-2002.php scroll down to "New
comp.lang.php newsgroup started")
Just because Kelly was rude, doesn't make you right. You top-posted
because you don't know how to use newsgroups. Who cares who created
comp.lang.whatever?
3. I've studied algorithm complexity analysis [I'm at Michigan
Engineering], I think that the problem lies in the fact that you
have a fundamental misunderstanding of what his function does. See
below.
So what? I major in Pure Mathematics at U. of M. Does that make me
know calculus or topology better than anybody out there? Get down, Mr
comp.lang.php's god.
Dimension7 wrote:
All,
I am comparing to functions to see which is "better". In better,I>> mean more efficient, optimize, faster, etc.
I have read other posts from other boards, but I'm not really

sure>> of the benefits or if there is a speed increase
with one vs. the other.


Your functions have the same performance.


False


It's never false or true when the OP doesn't post the code.
FIRST VERSION
function is_active($module) {
global $prefix, $dbi;
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
list ($act) = sql_fetch_row($result, $dbi);
Why are you using list() ? sql_fetch_row() doesn't exist.


He's using list because *sql_fetch_row returns an array, but he
doesn't want an array with just one value, so he's using list to
store that one value in a regular singular variable. Simple enough.


Where is the function prototype? How can you know if it's not defined
anywhere here? sql_fetch_row() is not a PHP core function. You might
have assumed right, but without the prototype, the two functions have
the same performance and if you don't know that, then you should take
Algorithm Analysis again and stick around for some DS classes.
if (!$result OR $act == 0) {
return 0;
} else {
return 1;
}
}

SECOND VERSION
function is_active($module) {
global $dbi;
static $save;
if (is_array($save)) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("select active from web_modules where
title='$module'", $dbi);
while ($act = sql_fetch_row($result, $dbi)) {
$save[$act[0]] = 1;
}


Why set 1 to $save[$act[0]] more than once? If while() will run
only once, why use while() then?


while() will run more than once, it will run as many times as there


You don't know that. You don't know what sql_fetch_row() returns
because the function doesn't exist or the OP didn't show. You guess.
You're a guesser.
// Ian Fette
// Proponent, comp.lang.php


Who cares? However stupidy you are, DO NOT TOP POST on USENET.
Jul 17 '05 #7
Tom Nielsen wrote:
On Fri, 30 Apr 2004 08:38:03 -0400 "Agelmar"
<if**********@comcast.net> wrote:
Kelly Thompson wrote:
On Fri, 30 Apr 2004 00:20:23 -0400
"Agelmar" <if**********@comcast.net> wrote:

This exact same question was posted on 4/25. I'm not sure why
you're> re-posting it... if you really want to find out, shove it
in a for> loop, run it 10,000 times, and time it.

What? Is that how you do? And why top-post? Maybe you should read
about how to answer messages on USENET and then study some
algorithm analysis basics.
[snip]
2. I top-posted because I felt like it. Before you chide me on the
rules of Usenet, you should know that I'm the one who created this
group.(http://www.php.net/news-2002.php scroll down to "New
comp.lang.php newsgroup started")


Just because Kelly was rude, doesn't make you right. You top-posted
because you don't know how to use newsgroups. Who cares who created
comp.lang.whatever?


Not comp.lang.whatever, *this group*.
3. I've studied algorithm complexity analysis [I'm at Michigan
Engineering], I think that the problem lies in the fact that you
have a fundamental misunderstanding of what his function does. See
below.


So what? I major in Pure Mathematics at U. of M. Does that make me
know calculus or topology better than anybody out there? Get down, Mr
comp.lang.php's god.


He implied that I needed to "study some algorithm analysis basics", to which
I replied that I already have.
Dimension7 wrote:
> All,
> I am comparing to functions to see which is "better". In better,
> mean more efficient, optimize, faster, etc.
> I have read other posts from other boards, but I'm not really
sure>> of the benefits or if there is a speed increase
> with one vs. the other.

Your functions have the same performance.


False


It's never false or true when the OP doesn't post the code.


From the rest of your post, I'm assuming you're referring to sql_fetch_row.
I assumed that he meant that as a generic *sql_fetch_row (e.g. could be
mysql_fetch_row, pg_fetch_row, mssql_fetch_row etc).
> FIRST VERSION
> function is_active($module) {
> global $prefix, $dbi;
> $result = sql_query("select active from web_modules where
> title='$module'", $dbi);
> list ($act) = sql_fetch_row($result, $dbi);

Why are you using list() ? sql_fetch_row() doesn't exist.


He's using list because *sql_fetch_row returns an array, but he
doesn't want an array with just one value, so he's using list to
store that one value in a regular singular variable. Simple enough.


Where is the function prototype? How can you know if it's not defined
anywhere here? sql_fetch_row() is not a PHP core function. You might
have assumed right, but without the prototype, the two functions have
the same performance and if you don't know that, then you should take
Algorithm Analysis again and stick around for some DS classes.


Given the code that he did post, assuming that both are semantically
correct, then you can assume that his sql_fetch_row returns an array because
he's assigning it to a list(), and that the while() will execute more than
once, otherwise it wouldn't be semantically correct. When someone posts a
function asking which version is better, I would tend to take as a basic
premise that the overall function is semantically correct...
> if (!$result OR $act == 0) {
> return 0;
> } else {
> return 1;
> }
> }
>
> SECOND VERSION
> function is_active($module) {
> global $dbi;
> static $save;
> if (is_array($save)) {
> if (isset($save[$module])) return ($save[$module]);
> return 0;
> }
> $result = sql_query("select active from web_modules where
> title='$module'", $dbi);
> while ($act = sql_fetch_row($result, $dbi)) {
> $save[$act[0]] = 1;
> }

Why set 1 to $save[$act[0]] more than once? If while() will run
only once, why use while() then?


while() will run more than once, it will run as many times as there


You don't know that. You don't know what sql_fetch_row() returns
because the function doesn't exist or the OP didn't show. You guess.
You're a guesser.
// Ian Fette
// Proponent, comp.lang.php


Who cares? However stupidy you are, DO NOT TOP POST on USENET.


"However stupidy" - wow, that's great. And just as an FYI, I hate netcops
like you. So I slipped from my usual routine and top-posted. So what? Who
made you the police of the Net?
Jul 17 '05 #8
I noticed that Message-ID: <c6************@ID-30799.news.uni-berlin.de>
from Agelmar contained the following:
Who cares? However stupidy you are, DO NOT TOP POST on USENET.


"However stupidy" - wow, that's great. And just as an FYI, I hate netcops
like you. So I slipped from my usual routine and top-posted. So what? Who
made you the police of the Net?


Guys, guys...

FWIW, I thought the top post appropriate in this instance. Do not top
post is not a universal rule - context matters and a one line response
at the bottom of a 100 line post is worse than this example.

However, picking up on typos and the general manner of that last
paragraph is not likely to promote harmony and peaceful co-existence.

Meanwhile, I /still/ don't have an answer to my ODBC problem...

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9
Geoff Berrow <bl******@ckdog.co.uk> wrote:
I noticed that Message-ID: <c6************@ID-30799.news.uni-berlin.de>
FWIW, I thought the top post appropriate in this instance. Do not top
post is not a universal rule - context matters and a one line response
at the bottom of a 100 line post is worse than this example.


Guess what: both are terrible practices :)

--

Daniel Tryba

Jul 17 '05 #10
Geoff Berrow wrote:
<snip>
Meanwhile, I /still/ don't have an answer to my ODBC problem...


What thread was that in?
Jul 17 '05 #11
I noticed that Message-ID: <c7************@ID-30799.news.uni-berlin.de>
from Agelmar contained the following:
Meanwhile, I /still/ don't have an answer to my ODBC problem...


What thread was that in?


Couple of weeks ago now

Subject: Interesting ODBC problem
Message-ID: <8p********************************@4ax.com>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
41 posts views Thread by Odd-R. | last post: by
7 posts views Thread by Chéraaar | last post: by
88 posts views Thread by William Krick | last post: by
19 posts views Thread by Dennis | last post: by
20 posts views Thread by Bill Pursell | last post: by
14 posts views Thread by v4vijayakumar | last post: by
25 posts views Thread by J Caesar | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.