473,574 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($modu le) {
global $prefix, $dbi;
$result = sql_query("sele ct 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($modu le) {
global $dbi;
static $save;
if (is_array($save )) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("sele ct 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 1950
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($modu le) {
global $prefix, $dbi;
$result = sql_query("sele ct 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($modu le) {
global $dbi;
static $save;
if (is_array($save )) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("sele ct 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*******@seve n.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_intertabl ecommand.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**********@c omcast.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($modu le) {
global $prefix, $dbi;
$result = sql_query("sele ct 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($modu le) {
global $dbi;
static $save;
if (is_array($save )) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("sele ct 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**********@c omcast.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
misunderstandin g 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($modu le) {
global $prefix, $dbi;
$result = sql_query("sele ct 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($modu le) {
global $dbi;
static $save;
if (is_array($save )) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("sele ct 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**********@c omcast.net> wrote:
Kelly Thompson wrote:
On Fri, 30 Apr 2004 00:20:23 -0400
"Agelmar" <if**********@c omcast.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.whate ver?
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 misunderstandin g 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($modu le) {
global $prefix, $dbi;
$result = sql_query("sele ct 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($modu le) {
global $dbi;
static $save;
if (is_array($save )) {
if (isset($save[$module])) return ($save[$module]);
return 0;
}
$result = sql_query("sele ct 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**********@c omcast.net> wrote:
Kelly Thompson wrote:
On Fri, 30 Apr 2004 00:20:23 -0400
"Agelmar" <if**********@c omcast.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.whate ver?


Not comp.lang.whate ver, *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 misunderstandin g 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($modu le) {
> global $prefix, $dbi;
> $result = sql_query("sele ct 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($modu le) {
> global $dbi;
> static $save;
> if (is_array($save )) {
> if (isset($save[$module])) return ($save[$module]);
> return 0;
> }
> $result = sql_query("sele ct 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
5862
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the...
41
3925
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course...
5
3453
by: jnikle | last post by:
I have two completely unrelated tables, one for reviews and another for pay periods. The reviews table has a review date in it, and the pay periods table is just a list of the beginnings of pay periods. What I need to do is take a given review date and find out which date in my pay periods table it's closest to, above or below. Any help...
7
1702
by: Chéraaar | last post by:
Hi, I have the following problem: In one file (addresses) I have a bunch of lines like this: New 0x8048240 Old 0x0: jne 804824a New 0x8048253 Old 0x0: je 8048293 New 0x80482c7 Old 0x0: jp 80482e0 ....
88
21983
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm wondering if one is better than the other or if there is some sort of hybrid of the two that would be superior. IMPLEMENTATION 1: #ifndef...
19
2635
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor = Color.Empty then..... or if mycolor is Color.Empty then .......
20
2127
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include...
14
4181
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
25
13002
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any...
0
7815
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7828
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6476
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5316
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3755
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2253
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.