473,418 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Performance question - passing data between objects (PHP4-style OOP)

Hello

A class that composes the output of shop-related data gets some info
from the main shop class. Now I wonder whether it is faster to store the
info in the output class or get it from the main class whenever it is
needed:

class shop_main {
var $prices = null;
function &get_prices() {
if (is_null($this->prices)) {
$this->prices =& $this->db->getCol('SELECT id FROM shop_prices');
if (!is_array($this->prices)) $this->prices = array();
}
return $this->prices;
}
}

// case 1: store info in class
class shop_output {
var $prices = null;
var $main_shop_obj;
function do_something($product_id) {
if (is_null($this->prices)) {
// assume that main shop object is already set
$this->prices =& $this->main_shop_obj->get_prices();
}
foreach ($this->prices as $price_id) {
// do something...
}
}
}

// case 2: get info each time it is needed
class shop_output {
var $main_shop_obj;
function do_something($product_id) {
// assume that main shop object is already set
foreach ($this->main_shop_obj->get_prices() as $price_id) {
// do something...
}
}
}

BTW I also wonder whether it is appropriate to pass the prices by
reference here - I assume it is, but in an older thread on performance
Chung Leung stated that passing by reference can decrease performance in
some cases.

Thanks for a comment!
Markus
Nov 9 '06 #1
5 1876
Markus Ernst wrote:
Hello

A class that composes the output of shop-related data gets some info
from the main shop class. Now I wonder whether it is faster to store the
info in the output class or get it from the main class whenever it is
needed:

class shop_main {
var $prices = null;
function &get_prices() {
if (is_null($this->prices)) {
$this->prices =& $this->db->getCol('SELECT id FROM shop_prices');
if (!is_array($this->prices)) $this->prices = array();
}
return $this->prices;
}
}

// case 1: store info in class
class shop_output {
var $prices = null;
var $main_shop_obj;
function do_something($product_id) {
if (is_null($this->prices)) {
// assume that main shop object is already set
$this->prices =& $this->main_shop_obj->get_prices();
}
foreach ($this->prices as $price_id) {
// do something...
}
}
}

// case 2: get info each time it is needed
class shop_output {
var $main_shop_obj;
function do_something($product_id) {
// assume that main shop object is already set
foreach ($this->main_shop_obj->get_prices() as $price_id) {
// do something...
}
}
}

BTW I also wonder whether it is appropriate to pass the prices by
reference here - I assume it is, but in an older thread on performance
Chung Leung stated that passing by reference can decrease performance in
some cases.

Thanks for a comment!
Markus
Impossible to tell from your comments. There are way too many variables.

For instance - if your main shop class has 10 values and you need each
of them 50 times, and those values don't change, it's probably more
efficient to cache them in the output class.

OTOH, if you have 1M items in the main class which change frequently and
you only need 2 items from it, it's more efficient to get them from the
main class each time.

And Chung is correct. Many times passing by reference is faster, but
occasionally it's slower. However, that should not be a reason you
decide to pass by reference or not. Or if it is, it should be about
number 247 on your list.

The real question is - are you actually having performance problems, or
are you spinning your wheels on something which may or may not be a
problem? I suspect the latter.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 9 '06 #2
Jerry Stuckle schrieb:
Markus Ernst wrote:
>Hello

A class that composes the output of shop-related data gets some info
from the main shop class. Now I wonder whether it is faster to store
the info in the output class or get it from the main class whenever it
is needed:

class shop_main {
var $prices = null;
function &get_prices() {
if (is_null($this->prices)) {
$this->prices =& $this->db->getCol('SELECT id FROM shop_prices');
if (!is_array($this->prices)) $this->prices = array();
}
return $this->prices;
}
}

// case 1: store info in class
class shop_output {
var $prices = null;
var $main_shop_obj;
function do_something($product_id) {
if (is_null($this->prices)) {
// assume that main shop object is already set
$this->prices =& $this->main_shop_obj->get_prices();
}
foreach ($this->prices as $price_id) {
// do something...
}
}
}

// case 2: get info each time it is needed
class shop_output {
var $main_shop_obj;
function do_something($product_id) {
// assume that main shop object is already set
foreach ($this->main_shop_obj->get_prices() as $price_id) {
// do something...
}
}
}

BTW I also wonder whether it is appropriate to pass the prices by
reference here - I assume it is, but in an older thread on performance
Chung Leung stated that passing by reference can decrease performance
in some cases.

Thanks for a comment!
Markus

Impossible to tell from your comments. There are way too many variables.

For instance - if your main shop class has 10 values and you need each
of them 50 times, and those values don't change, it's probably more
efficient to cache them in the output class.

OTOH, if you have 1M items in the main class which change frequently and
you only need 2 items from it, it's more efficient to get them from the
main class each time.
Thank you, this helps a lot as a guideline.
And Chung is correct. Many times passing by reference is faster, but
occasionally it's slower. However, that should not be a reason you
decide to pass by reference or not. Or if it is, it should be about
number 247 on your list.

The real question is - are you actually having performance problems, or
are you spinning your wheels on something which may or may not be a
problem? I suspect the latter.
Your suspection is correct. After I _had_ performance problems (30+
seconds execution time for displaying 20 or 30 items...) I started
caring for performance issues at all, and now I try to be aware of them
even before problems occur. With my poor technical background (I don't
have a basic IT education) this sometimes makes me spend too much effort
on questions of low priority. Thus, comments of the kind you posted are
very helpful for me, thanks!
Nov 9 '06 #3
Markus Ernst wrote:
Jerry Stuckle schrieb:
>Markus Ernst wrote:
>>Hello

A class that composes the output of shop-related data gets some info
from the main shop class. Now I wonder whether it is faster to store
the info in the output class or get it from the main class whenever
it is needed:

class shop_main {
var $prices = null;
function &get_prices() {
if (is_null($this->prices)) {
$this->prices =& $this->db->getCol('SELECT id FROM shop_prices');
if (!is_array($this->prices)) $this->prices = array();
}
return $this->prices;
}
}

// case 1: store info in class
class shop_output {
var $prices = null;
var $main_shop_obj;
function do_something($product_id) {
if (is_null($this->prices)) {
// assume that main shop object is already set
$this->prices =& $this->main_shop_obj->get_prices();
}
foreach ($this->prices as $price_id) {
// do something...
}
}
}

// case 2: get info each time it is needed
class shop_output {
var $main_shop_obj;
function do_something($product_id) {
// assume that main shop object is already set
foreach ($this->main_shop_obj->get_prices() as $price_id) {
// do something...
}
}
}

BTW I also wonder whether it is appropriate to pass the prices by
reference here - I assume it is, but in an older thread on
performance Chung Leung stated that passing by reference can decrease
performance in some cases.

Thanks for a comment!
Markus


Impossible to tell from your comments. There are way too many variables.

For instance - if your main shop class has 10 values and you need each
of them 50 times, and those values don't change, it's probably more
efficient to cache them in the output class.

OTOH, if you have 1M items in the main class which change frequently
and you only need 2 items from it, it's more efficient to get them
from the main class each time.


Thank you, this helps a lot as a guideline.
>And Chung is correct. Many times passing by reference is faster, but
occasionally it's slower. However, that should not be a reason you
decide to pass by reference or not. Or if it is, it should be about
number 247 on your list.

The real question is - are you actually having performance problems,
or are you spinning your wheels on something which may or may not be a
problem? I suspect the latter.


Your suspection is correct. After I _had_ performance problems (30+
seconds execution time for displaying 20 or 30 items...) I started
caring for performance issues at all, and now I try to be aware of them
even before problems occur. With my poor technical background (I don't
have a basic IT education) this sometimes makes me spend too much effort
on questions of low priority. Thus, comments of the kind you posted are
very helpful for me, thanks!
I understand. I've been programming since the late 60's, and have seen
my share of performance problems (think of what it was like to be on a
mainframe with 4,000 bytes - not 4K - of core memory, running at
something quite a bit less than 1Mhz clock rate).

And yes, you should always keep potential performance problems in mind
when programming. But I really don't worry about them until I get the
first cut at the program running and see if I have performance problems.

As an example - I wrote a fairly small C++ program with a lot of
recursive calculations (the theoretical max was 81! (81 factorial - 81 x
80 x 79 ... x 3 x 2 x 1) operations. The goal was to have it finish the
ops in something less than a couple of minutes on a 900Mhz laptop.

I went ahead and wrote it, not paying much attention to performance.
Results when I was done was every run was < 1 second. Mission accomplished.

Another case I had to parse out a rather large (5MB) html file in PHP.
The goal was to convert some huge tables to csv files for importing to a
spreadsheet. The first run took over 15 minutes. So at that time I
went back and optimized the code. Final results were around 20 seconds.

The bottom line was - I didn't waste any time on the first one because
it was already fast enough. The second one took me around 10% more time
to tweak and get the speed down. But that was much easier because I
already had the program working, and my design allowed me to easily
modify the code to close up the bottlenecks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 10 '06 #4
Jerry Stuckle schrieb:
Markus Ernst wrote:
>Jerry Stuckle schrieb:
[...]
>>The real question is - are you actually having performance problems,
or are you spinning your wheels on something which may or may not be
a problem? I suspect the latter.


Your suspection is correct. After I _had_ performance problems (30+
seconds execution time for displaying 20 or 30 items...) I started
caring for performance issues at all, and now I try to be aware of
them even before problems occur. With my poor technical background (I
don't have a basic IT education) this sometimes makes me spend too
much effort on questions of low priority. Thus, comments of the kind
you posted are very helpful for me, thanks!

I understand. I've been programming since the late 60's, and have seen
my share of performance problems (think of what it was like to be on a
mainframe with 4,000 bytes - not 4K - of core memory, running at
something quite a bit less than 1Mhz clock rate).

And yes, you should always keep potential performance problems in mind
when programming. But I really don't worry about them until I get the
first cut at the program running and see if I have performance problems.

As an example - I wrote a fairly small C++ program with a lot of
recursive calculations (the theoretical max was 81! (81 factorial - 81 x
80 x 79 ... x 3 x 2 x 1) operations. The goal was to have it finish the
ops in something less than a couple of minutes on a 900Mhz laptop.

I went ahead and wrote it, not paying much attention to performance.
Results when I was done was every run was < 1 second. Mission
accomplished.

Another case I had to parse out a rather large (5MB) html file in PHP.
The goal was to convert some huge tables to csv files for importing to a
spreadsheet. The first run took over 15 minutes. So at that time I
went back and optimized the code. Final results were around 20 seconds.

The bottom line was - I didn't waste any time on the first one because
it was already fast enough. The second one took me around 10% more time
to tweak and get the speed down. But that was much easier because I
already had the program working, and my design allowed me to easily
modify the code to close up the bottlenecks.
I actually started learning Java now - not because I am sure I will
write Java in the future, but because I hope that knowing a less
permissive, strictly typed, purely object oriented, compiled language
will help me write better PHP :-)
Nov 10 '06 #5
Markus Ernst wrote:
Jerry Stuckle schrieb:
>Markus Ernst wrote:
>>Jerry Stuckle schrieb:

[...]
>>>The real question is - are you actually having performance problems,
or are you spinning your wheels on something which may or may not be
a problem? I suspect the latter.

Your suspection is correct. After I _had_ performance problems (30+
seconds execution time for displaying 20 or 30 items...) I started
caring for performance issues at all, and now I try to be aware of
them even before problems occur. With my poor technical background (I
don't have a basic IT education) this sometimes makes me spend too
much effort on questions of low priority. Thus, comments of the kind
you posted are very helpful for me, thanks!


I understand. I've been programming since the late 60's, and have
seen my share of performance problems (think of what it was like to be
on a mainframe with 4,000 bytes - not 4K - of core memory, running at
something quite a bit less than 1Mhz clock rate).

And yes, you should always keep potential performance problems in mind
when programming. But I really don't worry about them until I get the
first cut at the program running and see if I have performance problems.

As an example - I wrote a fairly small C++ program with a lot of
recursive calculations (the theoretical max was 81! (81 factorial - 81
x 80 x 79 ... x 3 x 2 x 1) operations. The goal was to have it finish
the ops in something less than a couple of minutes on a 900Mhz laptop.

I went ahead and wrote it, not paying much attention to performance.
Results when I was done was every run was < 1 second. Mission
accomplished.

Another case I had to parse out a rather large (5MB) html file in PHP.
The goal was to convert some huge tables to csv files for importing to
a spreadsheet. The first run took over 15 minutes. So at that time I
went back and optimized the code. Final results were around 20 seconds.

The bottom line was - I didn't waste any time on the first one because
it was already fast enough. The second one took me around 10% more
time to tweak and get the speed down. But that was much easier
because I already had the program working, and my design allowed me to
easily modify the code to close up the bottlenecks.


I actually started learning Java now - not because I am sure I will
write Java in the future, but because I hope that knowing a less
permissive, strictly typed, purely object oriented, compiled language
will help me write better PHP :-)
Yep, Java is a good language, also. And I agree with you - the
techniques it uses can help quite a bit.

I've found it's always good to learn new languages (I stop counting at
15, many of which I no longer remember - and may no longer exist).
Different techniques can also lead you to different (and sometimes
better) approaches to a problem.

And I highly suspect you'll enjoy Java.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 10 '06 #6

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

Similar topics

0
by: momina_dar | last post by:
AlachiSoft “TierDeveloper” eradicates the problem of writing thousands of lines of code for your middle tier. Many software solutions on the market can do it for you but according to our research...
1
by: N Yiannakoulias | last post by:
Hi all, Is there a performance issue when passing data types of different sizes to a function? For example, if a double precision variable is bigger than a single precision variable, is it...
3
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing...
0
by: AliceSee | last post by:
I have two Application Domain A and B. I declare an MarshalByRef object called ObjectA in Domain A. I declare another MarshalByRef object called ObjectB in Domain B. ObjectB has a function...
25
by: Stuart Hilditch | last post by:
Hi all, I am hoping that someone with some experience developing nTier apps can give me some advice here. I am writing an nTier web app that began with a Data Access Layer (DAL), Business...
0
by: Viorel | last post by:
Working as a beginner with data objects in Visual Studio 2003 and C#, I use the "Generate Dataset" command in order to generate automatically the dataset objects based on data adapters. Generated...
1
by: msyez | last post by:
HI, I have 3 pages where I collect userdata that I want to show in a confirmationpage. Which is the best way of passing data from page1 to page4, page2 to page4 and so on? I like public property...
5
by: Rod | last post by:
I've written 2 ASP.NET applications (I've worked on one with a team and another by myself). In my ASP.NET pages, when saving data to a backend database I've done it by using the click event of a...
3
by: Marc Castrechini | last post by:
First off this is a great reference for passing data between the Data Access and Business Layers:...
8
by: rpsetzer | last post by:
I have to create a big web application and I was thinking of using a data layer. For each entity in the database, I'll define a class that maps the table structure, having sub-objects for each...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.