Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > objects vs sub routines in loop

Reply
Thread Tools

objects vs sub routines in loop

 
 
ed
Guest
Posts: n/a
 
      06-29-2003
Hi all. I need to create a script that is always going to be running.
So I guess pretty much everything is going to be taking place inside
of
one main loop.

If I use objects that are lexically scoped within that loop, it means
that
new objects have to be created at the beginning of every iteration.

Would using objects be much slower than just calling sub routines?

If the answer to that is yes. I wonder if one solution(as a way to
still use OOP)
would be to extend my classes and create a "clear()" method or
something similar
that would basically set my object back to an uninitialized state.
Then I reinitialize it at the top of the loop and use it as if it were
a new
object.

Example:

my $obj = new Obj;
while (!$done)
{
$obj->initialize('param1', 'param2');

$obj->doSomething();
$obj->doSomethingElse();

$obj->clear();
}

As opposed to:

while (!$done)
{
my $obj = new Obj('param1', 'param2');

$obj->doSomething();
$obj->doSomethingElse();
}

-----
Note: In a real situation the parameters to the objects will change
with
every interation. I just wanted to keep the examples simple.

----------

Would using the first example speed things up substantially?
Would it still be substantially slower than if I were just calling sub
routines
instead of creating objects?

Is object creation quick enough in perl that I should not even worry
about it?

tia,
--ed

 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      06-29-2003
ed wrote:

> Hi all. I need to create a script that is always going to be running.
> So I guess pretty much everything is going to be taking place inside
> of
> one main loop.
>
> If I use objects that are lexically scoped within that loop, it means
> that
> new objects have to be created at the beginning of every iteration.
>
> Would using objects be much slower than just calling sub routines?
>
> If the answer to that is yes. I wonder if one solution(as a way to
> still use OOP)
> would be to extend my classes and create a "clear()" method or
> something similar
> that would basically set my object back to an uninitialized state.
> Then I reinitialize it at the top of the loop and use it as if it were
> a new
> object.
>
> Example:
>
> my $obj = new Obj;
> while (!$done)
> {
> $obj->initialize('param1', 'param2');
>
> $obj->doSomething();
> $obj->doSomethingElse();
>
> $obj->clear();
> }
>
> As opposed to:
>
> while (!$done)
> {
> my $obj = new Obj('param1', 'param2');
>
> $obj->doSomething();
> $obj->doSomethingElse();
> }
>
> -----
> Note: In a real situation the parameters to the objects will change
> with
> every interation. I just wanted to keep the examples simple.
>
> ----------
>
> Would using the first example speed things up substantially?
> Would it still be substantially slower than if I were just calling sub
> routines
> instead of creating objects?
>
> Is object creation quick enough in perl that I should not even worry
> about it?
>

....
> --ed


Why don't you

use Benchmark;

and find out the answers to your speed questions on your own objects?
In my opinion, if you are really really concerned about speed, you
should probably code in C and forget OO altogether. It will probably be
the method calls that slow things down the most, regardless of the OO
language you code in. But if your methods themselves do significant
work, the overhead of the method calls could well be inconsequential.

It seems like you are putting too much emphasis on speed -- by the time
you complete your project, Moore's law will have made your speed
problems go away anyhow.

--
Bob Walton

 
Reply With Quote
 
 
 
 
ed
Guest
Posts: n/a
 
      06-30-2003
On Sun, 29 Jun 2003 19:44:43 GMT, Bob Walton
<(E-Mail Removed)> wrote:
>Why don't you
>
> use Benchmark;
>
>and find out the answers to your speed questions on your own objects?



I'm just starting on writing the OO stuff, so I thought maybe I should
ask before I write it all in an OO style.
I've really just recently started getting into OOP and I think
I'm really starting to like it. So ideally I'd like to use the OO
approach
for the program. But if the speed penalty was great, I could do
without object
oriented code.


>It seems like you are putting too much emphasis on speed -- by the time
>you complete your project, Moore's law will have made your speed
>problems go away anyhow.


You're probably right.
I think I'll just go ahead and use OOP for everything I wanted, in the
way
that seems natural(confining the object to the scope of the loop).
Then I'll benchmark and see if it seems like it should be faster, then
I'll worry about what
to do about it.

thanks,
--ed
 
Reply With Quote
 
llewelly
Guest
Posts: n/a
 
      06-30-2003
ed <(E-Mail Removed)> writes:

> On Sun, 29 Jun 2003 19:44:43 GMT, Bob Walton
> <(E-Mail Removed)> wrote:
>>Why don't you
>>
>> use Benchmark;
>>
>>and find out the answers to your speed questions on your own objects?

>
>
> I'm just starting on writing the OO stuff, so I thought maybe I should
> ask before I write it all in an OO style.
> I've really just recently started getting into OOP and I think
> I'm really starting to like it. So ideally I'd like to use the OO
> approach
> for the program. But if the speed penalty was great, I could do
> without object
> oriented code.

[snip]

w.r.t to performance of OO code, the best thing you can do is think
about whether OO is appropriate for your design. Use it where it fits.

if you need OO, you need OO, and emulating OO without using OO
language constructs is not likely to be faster than using OO
constructs directly.

To make an analogy, an array is faster than a hash if you don't need
key => value mappings, but if you do, the array just won't do the
job, and implementing a hash table by hand is not likely to result
in faster code than perl's builtin hashes.

Don't use OO where it's inappropriate - but recall that when you need
it, trying to wriggle out of the need is not likely to buy you
performance.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
how do make a pop-up in sub ASP.net sub ? THY ASP .Net 1 08-18-2003 11:30 PM



Advertisments