objects vs sub routines in loop

E

ed

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
 
B

Bob Walton

ed said:
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.
 
E

ed

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
 
L

llewelly

ed said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top